Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Passing an Object via selectio...

Replytotopic

Passing an Object via selection list

Posted in Forums : Ask a Rails expert

 
Profile

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

def page
  @thing = Thing.find(params[:id])
end
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” %>

<= submit_tag "Submit", :class => "submit" %>
<
end %>

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.

 
Me

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).

 
Profile

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)

Replytotopic

Other Recent Topics

Ask a Rails expert : activesupport string first method error

Ask a Rails expert : url_for broken ?

Ask a Rails expert : map.routes.rb pls help

Ask a Rails expert : how to h tag in controller

Ask a Rails expert : Problem with Restful routing and partial form

Ask a Rails expert : will_paginate, search and ajax

Ask a Rails expert : captcha

Ask a Rails expert : Mass Deletes in Admin Section

Ask a Rails expert : RJS partial rendering

Ask a Rails expert : problem configuring a particular route

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel