问题描述
在我们的Rails 4应用程序中,有四个模型:
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
要创建一个新的@calendar
,我们使用以下形式:
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 %>
当我们将此表单嵌入到用户的show.html.erb中(以便用户可以从其个人资料创建新的日历)时,一切正常.
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.
我们认为逻辑网址应为/users/:id/administrations.
We believe the logical url should be /users/:id/administrations.
因此,我们将以上表格嵌入了政府的index.html.erb中.
So, we embedded the above form in the Administration's index.html.erb.
执行此操作并访问实例/users/1/administrations时,就会出现以下错误:
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
编辑:这也是我们的管理员#Controller:
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?
推荐答案
原因是@calendar
不是在控制器操作中 已初始化 ,因此显然@calendar
是nil
.错误也是如此.
The reason is @calendar
is not initialised in your controller action, so obviously @calendar
is nil
. So is the error.
要获取 表格以在administrations/index.html.erb
中工作,您应该在administrations_controller
的index
操作中包含以下代码.
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
这篇关于Rails 4:表单中的第一个参数不能包含nil或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!