简体   繁体   中英

Populate Rails Form With Model Information From Another Controller

I've renamed the SessionsController and Session Model to Periods/Period because it conflicted with Devise and so you'll see this in the update

I have a sessions and events model/controller. When a new session is created, it needs to be associated with a particular event.

In my sessions model, I have an event_id, but I want to have a dropdown on the form that is populated with name of non-past events. Once that is selected, the form should be able to assign the correct event_id to the created session.

What is the correct way to go about doing this?

Here is my schema.rb to help you get a clearer picture of what the Models look like:

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

Here is my form:

<%= 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 %>

and I keep getting the following error: undefined method merge' for :name:Symbol`

Breaking out the various arguments of collection select: 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

Am I doing that correct?

To display a select menu with options from another model (not another controller), try collection_select .

In your new Session form, this might look like:

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

In the Sessions controller, in the create action, build the association like this:

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

I have found that this structure works for me:

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

The last bit was the html part which I used to set the Bootstrap class.

:name here could be :date or :street etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM