0

I have a form for creating new Missions with an Admin account. Each Mission is linked to an account. When I visit admin/missions/new I get an error "ArgumentError in Admin::Missions#new". Can anyone point to what I'm doing wrong?

When I checked the rails console a mission id and name does show up, but I guess I'm missing the Admin for the mission.

Here's my Controller

class Admin::MissionsController < Admin::ApplicationController
  def index
    @missions = missions
  end

  def new
    @mission = current_account.missions.new(params[:mission])

    @mission.save

  end

  def create
    render plain: params[:mission].inspect
  end


  def edit
    @mission = missions.find(params[:id])
  end




  private

  def missions 
    @missions ||= current_account.missions
  end
end

Here's my form

<%= form_with [:admin, @mission] do |f| %>
  <div>
    <label>Name</label>
    <%= f.text_field :name %>
  </div>

  <div>
  </div>

  <div>
    <%= f.submit %>
  </div>

<% end %>

I'm expecting the url admin/missions/new to take me to the form, but I I get the argument error wrong number of arguments (given 1, expected 0)

4

2 回答 2

0

理想情况下,新方法仅用于创建新实例,而创建方法用于保存记录。更改为 Ana Maria 的建议应该可以解决您的问题。谢谢。

于 2019-06-19T12:21:41.527 回答
0

您在新操作中还没有参数,因为视图(必须收集参数的表单所在的位置)是在控制器代码之后呈现的。该对象应在创建操作中创建:

def create
  current_account.missions.create(params[:mission])
end

新的动作应该是:

def new
  @mission = current_account.missions.new
end

检查Rails 指南的将表单绑定到对象部分

表单代码似乎也是错误的,因为form_with [:admin, @mission] do |f|它不是有效的语法。它应该是:

<%= form_with model: [:admin, @mission] do |form| %>

查看form_with文档以获取更多详细信息。

于 2019-06-19T11:44:08.517 回答