How do you do this in Rails?

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:

<div id="uplognavlist">
<% 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 %>
</div>

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

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

http://handyrailstips.com

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]

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):
<div id="uplognavlist">
<form name="channelsform" method="post" action="/ups/newchannel">
<div style="margin: 0pt; padding: 0pt;">
<input type="hidden" value="Ty9TBUArgAPxtLuvGL48Sg1uYbzlZWmSJL2dsWuL
+YQ=" name="authenticity_token"/>
</div>
<label for="ups_channel_id">Channel</label>
<select id="channel_channel_id" onchange="document.channelsform.submit
();" name="channel[channel_id]">
<option value=""/>
<option value="1">ggriptest</option>
</select>
</form>
<a title="Non-Automatic Change Log" style="float: right;" href="/
audits/list?update=UpLogBody">Audit</a>
<a title="Add or Remove Associates" style="float: right;" href="/ups/
hotwire?update=UpLogBody">Hotwire</a>
<a title="Show Uplog" style="float: right;" href="/ups/uplog?
update=UpLogBody">Uplog</a>
</div>

It appears that after either </select> or </form> there is some kind
of a line feed in the display.

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

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:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001910

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

http://handyrailstips.com

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

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

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

http://handyrailstips.com

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.