2012-08-07 65 views
0

我已经改名为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 

我这样做是否正确?

+0

见下文。总之,您需要使用collection_select而不使用“f”对象,它可以工作。 – 2012-08-08 14:51:26

回答

1

要显示带有来自其他型号(不是另一个控制器)的选件的选择菜单,请尝试collection_select

在新的会议形式,这可能是这样的:

collection_select(:event, :id, Event.where("date > :date", date: Time.now.strftime("%m/%d/%Y")) 

在会话控制器,在create行动,建立这样的关系:

@session.event = Event.find(params[:event][:id]) 
+0

这让我指出了正确的方向。我知道我需要使用collection_select,但我现在还没有工作。查看更新以获取更多信息。 – 2012-08-08 14:42:26

+0

仍然没有通过过滤日期过滤工作的形式,但通过删除f.collection_select,我能够得到它的工作。 – 2012-08-08 14:50:48

0

我发现,这结构适用于我:

<%= f.label :event %> 
     <%= f.collection_select :event_id, Event.all, :id, :name, {:prompt=> "Pick an Event"}, {:class => "form-control"} %> 

最后一位是html部分w我曾经设置Bootstrap类。

:name这里可能是:date:street等等

相关问题