# How do you do this in Rails?

**URL:** <https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972>\
**Category:** Rails\
**Created:** [June 24, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972 "2009-06-24T17:47:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [June 24, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972/1 "2009-06-24T17:47:21Z")

</div>

I have a model (“Ups”) and a corresponding controller  
(“UpsController”). The Ups model has a link to my “channels” toble  
through channel\_id

Everything I am going to do in the given views for Ups have to do with  
using those ups from a selected channel. Thus, I am putting a partial  
at the top of the views, with a select box to choose the channels:

\<% form\_for :ups, :html =\> {:name =\> 'channelsform'},:url=\>{ :action =\>"newchannel", :controller =\>"ups"} do |f| %\> \<%= f.label :channel\_id %\> \<%= select("channel", "channel\_id", Channel.find(:all, :order =\> 'channel ASC', :conditions =\> ['deleted=0']).collect {|p| [p.channel, p.id]}, {:include\_blank =\> true}) %\> \<%= f.submit "Change" , :class=\>'button' %\> \<% end %\> \<%= links\_for\_uplog %\>

My question is, within my UpsController.newchannel(), how do I access  
the calue of the channel\_id that has been selected in the form  
submission?

Incidentally, if you look at the erb code I put above, there is a line  
feed that gets inserted after the submit button, before the next  
section " links\_for\_uplog " within the div. How can I not have that?

Thanks, Janna

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [June 24, 2009, 6:47pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972/2 "2009-06-24T18:47:50Z")

</div>

Hi Janna,

First, you shouldn’t be calling Channel.find(:all, :order =\> ‘channel  
ASC’, :conditions =\> [‘deleted=0’]).collect {|p| [p.channel, p.id]}

I’d add a named scope to the Channel model like so:

named\_scope :not\_deleted, :conditions =\> {:deleted =\>  
false }, :order =\> ‘channel ASC’

And then in the controller:  
@channels = Channel.not\_deleted

Then in your view:  
\<%= f.select :channel\_id, @channels %\>

The standard here would be to create the new Ups in a method called  
create. By using form\_for on a new record rails will automatically  
know to post to an action called create so, unless you have a good  
reason not to, I’d recommend calling your controller method “create”  
and letting Rails behave the way it wants to.

which ‘line feed’ do you want to remove?

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com)**
>
> Ruby on Rails tips and tutorials

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [June 24, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972/3 "2009-06-24T18:51:56Z")

</div>

Sorry, to answer the original question:

> > My question is, within my UpsController.newchannel(), how do I access  
> > the calue of the channel\_id that has been selected in the form  
> > submission?

When rails posts a form to an action, all of the info from the form  
will be included in a hash called params.

In this case the hash would contain:  
:ups =\> {:channel\_id =\> value }

so to access it you’d use params[:ups][:channel\_id]

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [June 24, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972/4 "2009-06-24T18:58:57Z")

</div>

Gavin,

Thank you. But suppose I didn’t use a named scope – in that case, how  
then could I refer to it in my controller? (though I will follow your  
advice and use the named scope, I need to know the other way also).

Also, between:

\<%= f.submit “Change” , :class=\>‘button’ %\>  
\<% end %\>  
\<%= links\_for\_uplog %\>

it seems a line feed occurs…the \<%= links\_for\_uplog %\> is not  
immediately to the right as I would like it to be (in \<%=  
links\_for\_uplog %\>,

the rendered html appears as (copying from firebug):

Channel ggriptest Audit[Hotwire](https://www.ruby-forum.com/ups/%0Ahotwire?update=UpLogBody "Add or Remove Associates")[Uplog](https://www.ruby-forum.com/ups/uplog?%0Aupdate=UpLogBody "Show Uplog")

It appears that after either or there is some kind  
of a line feed in the display.

Can you please answer Me those two questions Gavin? Thank You, Janna

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [June 24, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972/5 "2009-06-24T19:41:13Z")

</div>

Try using \<%- end -%\> instead of \<% end %\> at the of the form. That  
should remove the line feeds and white space.

A lot of rails coders wouldn’t use the named scope here.  
I always try to keep as much of this sort of logic in the model but  
you could also simply use ‘find’ in the controller:

@channels = Channel.all( :conditions =\> { :deleted =\> false }, :order  
=\> ‘channel ASC’ ).collect { |p| p.name, p.id }

also check out collection\_select:

> **[ActionView::Helpers::FormOptionsHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001910)**
>
> Provides a number of methods for turning different kinds of containers into a set of option tags.

I think this might be more appropriate for you in this instance:

\<%= f.collection\_select :channel\_id, @channels, :id, :prompt =\> true  
%\>

Hope that helps?

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com)**
>
> Ruby on Rails tips and tutorials

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [June 24, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972/6 "2009-06-24T19:44:11Z")

</div>

damn - sorry that should be:  
\<%= f.collection\_select :channel\_id, @channels, :id, :channel, :prompt  
=\> true %\>

missed out the text\_method param

Gavin

[http://handyrailsitps.com](http://handyrailsitps.com)

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [June 24, 2009, 8:05pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972/7 "2009-06-24T20:05:20Z")

</div>

Okay - I’m assuming the action your form posts to is newchannel and  
you want to access the input from the form in that method?  
In which case:

@channel\_id\_you\_want = params[:ups][:channel\_id]

That should work?

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com)**
>
> Ruby on Rails tips and tutorials

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [June 24, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972/8 "2009-06-24T20:11:51Z")

</div>

Ahh, right…that’s it. God, I just couldn’t get that – ok, off to do  
it the right way that you showed me! -Janna.

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [June 24, 2009, 8:00pm UTC](https://www.ruby-forum.com/t/how-do-you-do-this-in-rails/170972/9 "2009-06-24T20:00:02Z")

</div>

Gavin,

Sorry to beat the dead horse, but I still need to know how to access  
the channel\_id given the way I initially posted the code. Can you show  
me that? -Janna
