그래서 저는 지금 Hartl 자습서를 주로 따르고 있습니다.하지만 사용자가 게시물을 올리면 게시물이 그에게 속할 것이고 간단한 방법으로 보여줄 수 있도록 만들고 싶습니다. 예를 들어 별도의 페이지. 트위터에 물건이 없어.사용자 게시물을 얻으려고 시도합니다
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post created!"
redirect_to root_url
else
render 'posts/index'
end
end
이제는 내 post_controller에 있습니다. 제출할 때 @ post에 게시하는 양식이 있습니다. 하지만 플래시 메시지가 나타나지 않아 제출을 눌러도 게시물이 저장되지 않았다는 의미입니다. 그 이유는 무엇보다 먼저, 나는 코드 라인도 얻지 못한다 : @post = current_user.posts.build(post_params)
. 그 빌드가 정확히 무엇입니까? 나는 단지 Post.new(post_params)?
을 수행하기로되어 있지 않습니까? 그러면 게시물이 사용자에게 속할 수 있도록 조금 수정 하시겠습니까? https://gist.github.com/Veske/7988593이
서버 로그 :
내가 내 사용자 및 사후 모델의 요점했다Started POST "/posts/index" for 127.0.0.1 at 2013-12-16 17:19:52 +0200
Processing by PostsController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iT08NreZv83WtImK6V9/jXZOpgtzwSLjlxB7T/wn0E4=", "text"=>"This is a s
post!", "commit"=>"Submit!"}
Rendered posts/index.html.erb within layouts/application (1.0ms)
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '390d667f7603eee5976fd8500a9a8776c
d3' LIMIT 1
Rendered layouts/_menu.html.erb (4.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 21ms (Views: 19.0ms | ActiveRecord: 1.0ms)
이것은 내 양식을
<%= form_tag(@post) do %>
<div class="forms">
<%= text_area_tag(:content, nil, placeholder: "Type text in here...")%>
<%= submit_tag 'Submit!' %>
</div>
<% end %>
빌드를 작동하는지는 correct..it의 현재 사용자에 대한 새 micropost을 생성합니다. 사용자 모델 및 게시 모델을 게시하십시오 ... user_id : integer가 마이크로 포스트 db 테이블의 열임을 확인하십시오. –
추적을 게시 한 경우, 즉 오류가있는 경우 문제를 발견하는 것이 더 간단했을 것입니다. 확인하기 위해,'user'와'post' 모델은'user has_many posts'와'post belongs_to user'에 의해 관련되어 있습니까? – vee
네, 요점을 편집하고 원래 게시물을 편집하면 모든 모델이 있습니다. – Veske