带有嵌套资源的 Rails 表单的 NoMethodError

问题描述

我创建了一个带有嵌套资源的简单表单,如下所示:

        <%= simple_form_for [@user,@spot] do |f| %>
          <div class="form-inputs">
            <%= f.input :location,label: false,input_html: { placeholder: "location" } %>
          </div>
          <div class="form-actions">
            <%= f.button :submit,"Submit",class: "btn-submit btn-red btn-fill" %>
          </div>
        <% end %>

在我的路线中,我进行了相应的嵌套:

  resources :users do
    resources :spots
  end

控制器中的方法

  def create
    @spot = Spot.new(spot_params)
    @spot.user = @user
    authorize @spot
    if @spot.save
      redirect_to root_path
    else
      render :new
    end
  end

在创建操作之前,设置用户

  def set_user
    @user = User.find(params[:user_id])
  end

用户 ID 包含在强参数中:

  def spot_params
    params.require(:spot).permit(:spot_date,:time,:location,:number,:bird_id,:user_id)
  end

我仍然收到 NoMethodError 并显示以下消息:

undefined method `spots_path' for #<#<Class:0x00007fba929a7028>:0x00007fba929a56d8>
Did you mean?  user_spot_path

在这里错过了什么?有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)