看看這個問題,我剛纔問:Comments on multiple models多態評論路線錯誤
到目前爲止,我所擁有的一切設置正確,我的意見表,我的模型和控制器。
但是,我遇到了一些路由錯誤。
在我的路線文件:
resources :posts do
resources :comments do
member do
put "like", to: "comments#upvote"
end
end
end
resources :books do
resources :comments do
member do
put "like", to: "comments#upvote"
end
end
end
我的評論形式:
<% form_for [@commentable, Comment.new] do |f| %>
<p>
<%= f.text_area :body %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
這是我得到的錯誤:
未定義的方法`comments_path」
任何想法如何讓路線工作?
編輯:
這是我在我的意見控制器:
def index
@commentable = find_commentable
@comments = @commentable.comments
end
def create
@commentable = find_commentable
@comment = @commentable.comments.build(params[:comment])
if @comment.save
flash[:notice] = "Successfully created comment."
redirect_to :id => nil
else
render :action => 'new'
end
end
private
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
什麼是'@ commentable'變量? –
剛剛添加了一個編輯問題,顯示我在控制器中的內容 –
您確定在任何控制器操作正在呈現初始評論表單時定義了「@ commentable」嗎? –