创建嵌套表单:"undefined method `push'"
Create a nested form : "undefined method `push'"
我想构建一个新的评论表单,但是 return 出现了一个我不明白的错误。你能给我解释一下问题吗?
我的代码
路线:
resources :posts do
resources :pushs do
resources :reviews
end
end
link :
<%= link_to 'Add comment', new_post_push_review_path(@push.post_id, @push) %>
我想要构建的表单:
<%= simple_form_for([@post, @post.push.reviews.build]) do |f| %>
<%= f.input :rating %>
<%= f.input :comment %>
<%= f.button :submit %>
<% end %>
& 最后,控制器审核:
class ReviewsController < ApplicationController
before_action :authenticate_user!
before_action :find_push
before_action :find_post
def new
@review = Review.new
@pushs = Push.all
end
def create
@push = Push.find(params[:review][:id])
@review = Review.new(review_params)
@review.post_id = @push.post_id
@review.push_id = @push.id
@review.user_id = current_user.id
if @review.save
redirect_to push_path(@push.post_id, @push)
else
render 'new'
end
end
private
def review_params
params.require(:review).permit(:rating, :comment)
end
def find_post
@post = Post.find(params[:post_id])
end
def find_push
@post = Post.find(params[:post_id])
@push = @post.pushs.find(params[:push_id])
end
end
好吧,如果你有任何想法来解释我的错误,那就太好了!
在您的路线中,您必须将 resources :pushs do 更改为 resources :pushes do
并且您可能没有在 Post、推送、评论模型中设置关联。
我想构建一个新的评论表单,但是 return 出现了一个我不明白的错误。你能给我解释一下问题吗?
我的代码
路线:
resources :posts do
resources :pushs do
resources :reviews
end
end
link :
<%= link_to 'Add comment', new_post_push_review_path(@push.post_id, @push) %>
我想要构建的表单:
<%= simple_form_for([@post, @post.push.reviews.build]) do |f| %>
<%= f.input :rating %>
<%= f.input :comment %>
<%= f.button :submit %>
<% end %>
& 最后,控制器审核:
class ReviewsController < ApplicationController
before_action :authenticate_user!
before_action :find_push
before_action :find_post
def new
@review = Review.new
@pushs = Push.all
end
def create
@push = Push.find(params[:review][:id])
@review = Review.new(review_params)
@review.post_id = @push.post_id
@review.push_id = @push.id
@review.user_id = current_user.id
if @review.save
redirect_to push_path(@push.post_id, @push)
else
render 'new'
end
end
private
def review_params
params.require(:review).permit(:rating, :comment)
end
def find_post
@post = Post.find(params[:post_id])
end
def find_push
@post = Post.find(params[:post_id])
@push = @post.pushs.find(params[:push_id])
end
end
好吧,如果你有任何想法来解释我的错误,那就太好了!
在您的路线中,您必须将 resources :pushs do 更改为 resources :pushes do
并且您可能没有在 Post、推送、评论模型中设置关联。