You are here: Forums Ask a Rails expert Passing an Object via selectio...
Posted in Forums : Ask a Rails expert
Authority 0
Posting Rating 0
Sign in to rate this post
|
Let’s say I wanted a to have all of my Thing objects in a selection list. Then, when a thing is selected, it is passed to page.html.erb, and the name of that thing is displayed. Here’s what I have: class MainController < ApplicationController def index @things = Thing.find_all_things #method in thing.rb end
Then in index.html.erb: <% form_for :thing, :url => { :action => :page} do |form| %> <%= form.select :thing, @things, #contains all things :prompt => “Select a thing” %>
Then in page.html.erb: <%= @thing.name %> One problem is that in the selection list it only displays the object, not the name—<thing:0x465baf4>. A solution would be to display by the names, and then find_by_name in page.html.erb, but that seems like extraneous work. The second problem is that it doesn’t work. Thank you for any help. EDIT: can’t figure out how to format this thing correctly. All apologies. |
Authority 62
Posting Rating 100
Sign in to rate this post
|
<% form_for :thing, :url => { :action => :page } do |f| %>
<%= f.collection_select :thing_id, @things, :id, :name %>
<%= f.submit "Submit" %>
<% end %>
Something like that should work. Take a look at the collection_select documentation if you need to. PS: You can do nice highlighting inside <.pre> <./pre> (without the periods). |
Authority 0
Posting Rating 0
Sign in to rate this post
|
Got it working. Thanks so much. Also had to change @thing = Thing.find(params[:id]) to @thing = Thing.find(params:thing) |
Ask a Rails expert : Use Rails to develop sites for both Designer and Programmer
Ask a Rails expert : Rails+RS232
Ask a Rails expert : Is this a good way to add Admin section
Ask a Rails expert : RSS feed maker in rails 2.1
Ask a Rails expert : Syncing with ugly legacy databases
Ask a Rails expert : juggernaut Error
Ask a Rails expert : gem "chronic" error
Ask a Rails expert : gem install error
Ask a Rails expert : need your help or views for distributed programming with ruby
Ask a Rails expert : how to refresh ruby files without restart production server