Rails 4: First argument in form cannot contain nil or be empty

限于喜欢 提交于 2019-12-12 03:35:04

问题


In our Rails 4 app, there are four models:

class User < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :calendars, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Calendar < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :users, through: :administrations
end

class Post < ActiveRecord::Base
    belongs_to :calendar
end

We have routed the corresponding resources with Routing Concerns, as follows:

concern :administratable do
  resources :administrations
end

resources :users, concerns: :administratable
resources :calendars, concerns: :administratable

To create a new @calendar, we use the following form:

<h2>Create a new calendar</h2>
<%= form_for(@calendar) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :name, placeholder: "Your new calendar name" %>
  </div>
  <%= f.submit "Create", class: "btn btn-primary" %>
<% end %>

When we embed this form into the user's show.html.erb (so that a user can create a new calendar from his profile), everything works fine.

However, we would like to have a special page, call dashboard, where a user could see all his calendars and create a new one.

We believe the logical url should be /users/:id/administrations.

So, we embedded the above form in the Administration's index.html.erb.

And as soon as we do that and visit for instance /users/1/administrations, we get the following error:

ArgumentError in AdministrationsController#index

First argument in form cannot contain nil or be empty

Extracted source (around line #432):

else
  object      = record.is_a?(Array) ? record.last : record
  raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
  object_name = options[:as] || model_name_from_record_or_class(object).param_key
  apply_form_for_options!(record, object, options)
end

EDIT: here is also our Administrations#Controller:

class AdministrationsController < ApplicationController

  def to_s
    role
  end

  def index
    @user = current_user
    @administrations = @user.administrations
  end

  def show
    @administration = Administration.find(params[:id])
  end

end

Any idea of what we are doing wrong?


回答1:


First argument in form cannot contain nil or be empty

The reason is @calendar is not initialised in your controller action, so obviously @calendar is nil. So is the error.

To get your form to work in administrations/index.html.erb, you should be having the below code in your index action of your administrations_controller.

def index
  @user = current_user
  @administrations = @user.administrations
  @calendar = Calendar.new # this one.
end


来源:https://stackoverflow.com/questions/31284055/rails-4-first-argument-in-form-cannot-contain-nil-or-be-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!