사용자가 게시물에 댓글을 달 수있는 앱을 만들고 있습니다. 게시물을 표시 할 때 그러나, 나는 오류가 점점 오전 : ArgumentError in PostsController#show First argument in form cannot contain nil or be empty
Ruby on Rails 오류 : 정의되지 않은 메소드`comments_path '
그것은 내가 여기 render 'shared/comment_form'
시도 라인에 실패한 것입니다 내 게시물/show.html.erb :
<% provide(:title, @post.title) %>
<div class="post-details">
<div class="post-title">@post.title</div>
<div class="post-url">@post.url</div>
<div class="post-content">@post.content</div>
<%= render 'shared/comment_form' if logged_in? %>
<% if @post.comments.any? %>
<h3>Comments</h3>
<ol class="comments">
<%= render @comments %>
</ol>
<% end %>
</div>
내 posts_controller.rb :
클래스가 PostsController <와 ApplicationController before_action : LOGGED_IN_USER이 만 : [: 작성 : 파괴]
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def show
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :url)
end
end
을 그리고 여기 내 공유/_comment_form.html.erb 부분 :
<%= form_for(Comment.new) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new comment..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
그리고 내 comments_controller.rb :
class CommentsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def create
@comment = current_user.comments.build(comment_params)
if @comment.save
flash[:success] = "Comment created!"
redirect_to request.referrer || root_url
else
render 'new'
end
end
def new
@comment = Comment.new
end
def destroy
@comment.destroy
flash[:success] = "Comment deleted"
redirect_to request.referrer || root_url
end
private
def comment_params
params.require(:comment).permit(:content)
end
end
여담 : 나는 그것의 관습하지 내년 Comment.new
를 사용하는 알- 그 문제도 있습니다. 그러나 한 번에 한 가지 문제를 해결하고 싶습니다. (아무렇지도 않게 참여하지 않는 한)
사실은 중요합니다. 그러나 지금은 오류가 발생합니다 " 'nil'ActiveModel 호환 개체가 아닙니다. 구현해야합니다 : to_partial_path" –
Ok ... 이제 문제는이 줄'<% = render @comments %> '% @ render @ post.comments %>' – keyzee
당신이 옳았어요 -'@ post'를 통해'comments'에 액세스해야했습니다. –