我已经改名为SessionsController和会话模型周期/周期,因为它与冲突设计,所以你会看到这样的更新填充Rails的形式随着模型的信息从另一个控制器
我有一个会议和事件模型/控制器。当创建新会话时,它需要与特定事件关联。
在我的会话模型中,我有一个event_id,但我希望在填充名称为非过去事件的表单上有一个下拉列表。一旦选择了该选项,表单应该能够将正确的event_id分配给创建的会话。
要做到这一点,正确的方法是什么?
这是我schema.rb来帮助你的模型是什么样子更清晰的画面:
ActiveRecord::Schema.define(:version => 20120807154707) do
create_table "events", :force => true do |t|
t.string "name"
t.date "date"
t.string "street"
t.string "city"
t.string "state"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "sessions", :force => true do |t|
t.string "name"
t.integer "event_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "admin", :default => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
end
这里是我的形式:
<%= form_for(@period) do |f| %>
<%= f.label :Name %>
<%= f.text_field :name%>
<%= f.label :Event %>
<%= f.collection_select(:period, :event_id, Event.all, :id, :name)%>
<%= f.label :time %>
<%= f.text_field :time, id: "timepicker" %>
<%= f.submit "Create Event" %>
<% end %>
,我不断收到以下错误:undefined method
合并'为:名称:符号'
分解收集选择的各种参数:f.collection_select(:period, :event_id, Event.all, :id, :name)
:period -> The object
:event_id -> the method I want to set on the object.
Event.All -> The collection (for now I'll take all of them)
:id -> the value of the html element option
:name -> the value displayed to the user
我这样做是否正确?
见下文。总之,您需要使用collection_select而不使用“f”对象,它可以工作。 – 2012-08-08 14:51:26