未定义的方法`_path

时间:2015-11-09 15:07:43

标签: ruby-on-rails

我在创建新对象方面遇到了一些问题,称为" round"。我收到以下错误:

  

Rounds#new中的NoMethodError

     

未定义的方法`rounds_path'

rounds_controller.rb:

  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

查看行动新:

      %h2 New round
      = simple_form_for @round do |r|
        = r.input :number, label: 'Round number'
        = r.button :submit, class: "btn btn-primary"

routes.rb中:

  resources :tournaments do
    resources :rounds
  end

我认为问题在于嵌套,但我不知道究竟在哪里。

2 个答案:

答案 0 :(得分:2)

Rails猜测路线,这很有用。因为你只绕过它的路线rounds_path,所以你怀疑它是因为你已经嵌套了它。改变这个

= simple_form_for @round do |r|

= simple_form_for [@tournament, @round] do |r|

正如马克斯所说,你需要参加锦标赛。我假设您已在控制器中使用before_action完成此操作。然后我会亲自通过锦标赛创造轮次

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

答案 1 :(得分:1)

您可以指定简单表单将发布到的网址。

%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"

为了将来参考,您可以在终端上运行routes.rb来检查rake routes创建的实际路线。