Rails 4: First argument in form cannot contain nil or be empty -
in our rails 4 app, there 4 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 corresponding resources routing concerns, follows:
concern :administratable resources :administrations end resources :users, concerns: :administratable resources :calendars, concerns: :administratable
to create new @calendar
, use following form:
<h2>create new calendar</h2> <%= form_for(@calendar) |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 embed form user's show.html.erb (so user can create new calendar profile), works fine.
however, have special page, call dashboard, user see calendars , create new one.
we believe logical url should /users/:id/administrations.
so, embedded above form in administration's index.html.erb.
and , visit instance /users/1/administrations, following error:
argumenterror in administrationscontroller#index first argument in form cannot contain nil or 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 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 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 doing wrong?
first argument in form cannot contain nil or empty
the reason @calendar
not initialised in controller action, @calendar
nil
. error.
to your form work in administrations/index.html.erb
, should having below code in index
action of administrations_controller
.
def index @user = current_user @administrations = @user.administrations @calendar = calendar.new # one. end
Comments
Post a Comment