Question: Help with select tag

I want to create a select tag from a table. Looking through the API
don't believe I could find it.
Table has an id and label. Labels would be displayed, id would be saved.

TIA
Stuart

a trivial example:

<%= select("node","node_type_id", NodeType.find(:all,:order
=> "name").collect{|node| [node.name, node.id]}) %>

Dark Ambient escribió:

Thanks I wound up going with this :

<%=
@states = State.find(:all, :order => "name")
collection_select(:state, :name, @states, :id, :name)
%>

Just wondering , this is in the view , I probably should move this to
the model, i.e.
def list_states
.......
end

Or helper or controller, I realize I have all options but since I'm a
rails newb open for suggestions.

Stuart

Now I'm having a problem. In my controller I have this:
def state
@states = State.find(:all, :order => "name")
end

When I try to access the @states in the view I get a nil error:
<%=
collection_select(:state, :name, @states, :id, :name)
%>

What am I missing ?

TIA
Stuart

I think you need to do that in the action before you use it. I mean, it
can't be in 'state' cos that is called when you go to:
http://localhost:3000/controller/state

If you want it to be accessed in other places, you will need to put it
in those functions, places like 'new', 'create', 'update' etc..

Hope this helps,
Cheers
Mohit.

Dark Ambient wrote:

Most likely @states doesn’t exist when the view is rendered. In the standard scaffold, you must setup @states in the edit, new and [for the validation errors] in the error part of the update actions.

Regards,

Thomas

The API doc is a bit confusing on collection_select:
collection_select(object, method, collection, value_method,
text_method, options = {}, html_options = {})
So object I'd think would be State and method I could call list_state
(in the controller)
or maybe I'm better off putting in the model though that hasn't worked
either yet.

Stuart

I had yesterday a similar problem; because I thought I might need exactly this list in other tables, too I did this to application_helper.rb:

module ApplicationHelper
def country_select(parent, fk = :country_id, opt = {:include_blank => true})

countries = Country.find :all, :order => :name
collection_select(parent, fk, countries, :id, :name, opt)

end
end

Country

<%= country_select :breeder %>

But note that I’m also new to ruby & rails - I’m sure there are much better approaches. e.g. this misses some handling of the default opt parameter.

Thomas

I'm not following the new, edit or error idea.
Stuart

Incorrect.

Lets say you have a model "address", that contains a column "state_id":

@states = State.find(:all)

Then in your view:

collection_select ("address", "state_id", :id, :name)

When the form is posted, the id of the selected "state" will be
@address.state_id (assuming @address = Address.new(params[:address]))

Maybe this is more along the lines of what you are looking to do?

Thanks but no , still getting a nil object.
What I don't get up the CRUD reference (above) is that all I"m trying
to do is create the list / array and access it from the form. It's not
going to be updated , merely a form element to get values from .

Stuart

Maybe, but I think this is similar to what I'm doing already.
I have a model State (table states)
column names are id and state

so this in the view works:
<%=
@states = State.find(:all, :order => "name")
collection_select(:state, :name, @states, :id, :name)
%>

Yet, moving the find to a model , controller or helper is not working.

Am I missing something in everyones responses ?
Stuart

I found a solution and not sure if it's the right one but .......
In the helper for the associated controller:

def liststates
    @states = State.find(:all, :order => "name")
end

Then in the view:

collection_select(:state, liststates, @states, :id, :name)

Stuart

Stuart, if you put it into an action called 'state' in the controller,
the variable @states is visible only in that action (state)
So, if you access the URL:
http://localhost:3000/controller/state then @states is visible.

If you want it to be available in the action 'new', you want to load the
variable @states before you render the view for it. that means it needs
to be a part of the 'new' action like this:
  def new
    @states = State.find_all
    @clinic = Clinic.new
  end

This way, the @states variable is visible in the view for new. Does
this make sense?

As to where it should be moved, "code" within views belongs in helpers.

Cheers
Mohit.

Dark Ambient wrote:

Thanks Mohit, and that is where it is now .
Stuart

That is correct - the only "code" in a view should be stored in a helper
function.
If you need it more than 1 controller, consider adding it to the
application helper.

Cheers
Mohit.

Dark Ambient wrote: