我在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
答案 0 :(得分:1)
希望你有@case
值,试试这个:
<%= form_for([@case, @round]) do |f| %>
<%= render 'shared/error_messages', object: @round %>
...
<%= f.submit "Submit", class: "btn btn-primary form-button case-button" %>
<% end %>
<强>更新强>
def new
@side_options = @case.sides.map { |s| [s.name, s.id] }
@round = @case.rounds.new
end
private
def set_case
@case = Case.find(params[:case_id])
end
答案 1 :(得分:1)
问题是我的@case是零,所以我将@case = Case.find(params[:case_id])
添加到我的创建操作的else分支