Undefined method `_path
我在创建名为"round"的新对象时遇到了一点问题。我收到以下错误:
NoMethodError in Rounds#new
undefined method `rounds_path'
rounds_controller.rb:
1 2 3 4 5 6 7 8 9 10 11 12 13 | def new @round = Round.new end def create @round = Round.new(round_params) end private def round_params params.require(round).permit(:tournament_id) end |
查看操作新:
1 2 3 4 | %h2 New round = simple_form_for @round do |r| = r.input :number, label: 'Round number' = r.button :submit, class:"btn btn-primary" |
Routes.rb:
1 2 3 | resources :tournaments do resources :rounds end |
我认为问题在于嵌套,但我不知道具体在哪里。
Rails 猜测路线,这很有用。因为你只是绕过它路由到
1 | = simple_form_for @round do |r| |
到
1 | = simple_form_for [@tournament, @round] do |r| |
正如 Max 所说,您需要获得锦标赛。我假设您已经在控制器中使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class RoundsController before_action :set_tournament def new @round = @tournament.rounds.new end def create @round = @tournament.rounds.new(round_params) end private def set_tournament @tournament = Tournament.find(params[:tournament_id]) end def round_params params.require(:round).permit(:number) end end |
您可以指定简单表单将发送到的 url。
1 2 3 4 | %h2 New round = simple_form_for @round, url: tournament_rounds_path(tournament_id: params[:tournament_id]) do |r| = r.input :number, label: 'Round number' = r.button :submit, class:"btn btn-primary" |
为了将来参考,您可以通过在终端上运行