# Question: Help with select tag

**URL:** <https://discuss.rubyonrails.org/t/question-help-with-select-tag/624>\
**Category:** rubyonrails-talk\
**Created:** [August 28, 2006, 1:02pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624 "2006-08-28T13:02:17Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [August 28, 2006, 1:02pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/1 "2006-08-28T13:02:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matias\_Surdi](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@Matias\_Surdi](https://discuss.rubyonrails.org/u/Matias_Surdi)\
**Post date:** [August 28, 2006, 1:57pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/2 "2006-08-28T13:57:43Z")

</div>

a trivial example:

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

Dark Ambient escribió:

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [August 28, 2006, 2:56pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/3 "2006-08-28T14:56:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [August 28, 2006, 3:19pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/4 "2006-08-28T15:19:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Mohit\_Sindhwani](https://discuss.rubyonrails.org/u/Mohit_Sindhwani)\
**Post date:** [August 28, 2006, 3:39pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/5 "2006-08-28T15:39:13Z")

</div>

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](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:

---

<div class="post-metadata">

**Author:** ![T\_wenrich](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@T\_wenrich](https://discuss.rubyonrails.org/u/T_wenrich)\
**Post date:** [August 28, 2006, 3:43pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/6 "2006-08-28T15:43:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [August 28, 2006, 4:01pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/7 "2006-08-28T16:01:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![T\_wenrich](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@T\_wenrich](https://discuss.rubyonrails.org/u/T_wenrich)\
**Post date:** [August 28, 2006, 4:12pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/8 "2006-08-28T16:12:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [August 28, 2006, 4:15pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/9 "2006-08-28T16:15:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_Scappa](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@Michael\_Scappa](https://discuss.rubyonrails.org/u/Michael_Scappa)\
**Post date:** [August 28, 2006, 4:16pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/10 "2006-08-28T16:16:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [August 28, 2006, 4:23pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/11 "2006-08-28T16:23:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [August 28, 2006, 4:33pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/12 "2006-08-28T16:33:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [August 28, 2006, 4:49pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/13 "2006-08-28T16:49:24Z")

</div>

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

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

Then in the view:

collection\_select(:state, liststates, @states, :id, :name)

Stuart

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Mohit\_Sindhwani](https://discuss.rubyonrails.org/u/Mohit_Sindhwani)\
**Post date:** [August 28, 2006, 4:50pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/14 "2006-08-28T16:50:06Z")

</div>

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](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: &nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@states = State.find\_all &nbsp;&nbsp;&nbsp;&nbsp;@clinic = Clinic.new &nbsp;&nbsp;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:

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [August 28, 2006, 5:02pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/15 "2006-08-28T17:02:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Mohit\_Sindhwani](https://discuss.rubyonrails.org/u/Mohit_Sindhwani)\
**Post date:** [August 28, 2006, 5:14pm UTC](https://discuss.rubyonrails.org/t/question-help-with-select-tag/624/16 "2006-08-28T17:14:19Z")

</div>

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:
