【发布时间】:2015-07-26 14:56:24
【问题描述】:
我有一个rounds 资源嵌套在cases 资源中,所以我的routes.rb 文件如下所示:
resources :cases do
resources :rounds
end
我在控制器中的create 操作如下所示:
def create
@round = Round.new(round_params)
if @round.save
flash[:success] = "Round added"
redirect_to case_path(id: @round.case_id)
else
render 'new'
end
end
我的_round_form.html.erb 视图如下所示:
<%= form_for [@case, @round] do |f| %>
<%= render 'shared/error_messages', object: @round %>
...
<%= f.submit "Submit", class: "btn btn-primary form-button case-button" %>
<% end %>
由于某种原因,当我正确创建一个回合时,它可以工作,但是当我提交一个有错误的回合时,它没有呈现错误消息,而是给了我这个错误:
undefined method rounds_path' for #<#<Class:0xc8b7508>:0xc8b6da0>
为什么会这样?它不应该寻找new_case_round_path,而不是rounds_path吗?
新的行动,如果有帮助的话:
def new
id = params[:case_id]
unless id
flash[:danger] = "You must pick a case before trying to log a round"
redirect_to 'pick_case'
end
@case = Case.find(id)
@side_options = @case.sides.map { |s| [s.name, s.id] }
@round = Round.new
end
【问题讨论】:
-
您的
@case为零。您在哪个视图页面/操作中调用_round_form.html.erb? -
我在
new.html.erb中渲染round_form -
你是怎么得到
params[:case_id]的? -
刚刚意识到我的错误,你是对的,我使用 params[:case_id] 来修复它。我会将更改发布为答案
标签: ruby-on-rails