Possible name clash?

Scenario:

Table in database has two fields, id and category (table name is:
categories)
Controller is named category_controller.rb
Model is named category.rb
Helper is named category_helper.rb
Form is named list.rhtml in view\category directory

The code in play on the form is (from generating scaffold):

<% for category in @categories %>

<%= h(category.category) %> <%= link_to 'Show', :action => 'show', :id => category %> <%= link_to 'Edit', :action => 'edit', :id => category %> <%= link_to 'Delete', { :action => 'destroy', :id => category }, :confirm => 'Are you sure?', :post => true %> <% end %>

link_to on form is to action destroy

code that was in action in category_controller (on first attempt that
did not work):

cat = params[:category]

Problem is: cat was nil

Now I worked around it by doing this:

@category = Category.find(params[:id])
cat = @category.category

So, why if I can get the value of params[:id] from the form cannot I not
also get the value of param[:category]?

Is it possible I am getting a name clash here? And if not a name clash,
then of course the question becomes what might it be?

There doesn’t seem to be any confusion in the form with this line:

<%= h(category.category) %>
But then the context seems such that there wouldn’t be, but perhaps the
context is not so clear in the action in the controller?

Hi Scott,

The reason the category is not available is that you are not passing
it in your link_to. To pass it just add a :category =>
category.category to your link_to params.

This line: <%= h(category.category) %>
will only display the category, but will not send it up when someone
clicks on the links.

Tom

On 7/7/06, Scott H. [email protected] wrote:

link_to on form is to action destroy
@category = Category.find(params[:id])
But then the context seems such that there wouldn’t be, but perhaps the
context is not so clear in the action in the controller?


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Tom D.

http://atomgiant.com
http://gifthat.com

On Jul 7, 2006, at 6:48 AM, Scott H. wrote:

<% end %>

link_to on form is to action destroy

code that was in action in category_controller (on first attempt that
did not work):

cat = params[:category]

Problem is: cat was nil

when you have a construct like “:id => category”, the value given to
the params[:id] is obtained from category.to_param which is usuallly
the same as category.id, that is, the primary key for the model.
This particular bit of magic was clarified by Dave T. in response
to my question last fall at a No Fluff Just Stuff conference (yeah,
Java-centric, but a surprising amount of Ruby and Rails thrown in
there).

-Rob

Hi Scott,

Scott H. wrote:

Anyway, I’m still somewhat confused as I could
have swore I read in one of my books that all the
field values on the form are returned and available
to the controller, automatically done for you and
each field then available to you through
params[:some_name]. Did I misunderstand or
read it wrong?

Two related items seem to me to be at the root of your current problem,
one
of which you’ve already figured out. Forms are not pages, they’re
‘containers’ (that are part of a page) that start and end with explicit
tags. The second item is about ‘fields’. Your understanding above is
correct. I think the problem you’re having here is your understanding
of
the term ‘field’. Your view contained no fields in the form-sense of
the
word. A form field is an object like a text_field, a check_box, a
radio_button, etc. The user-entered values of those things will be
passed
back to the controller in the params hash when the form is submitted.

hth,
Bill

Thanks Tom and Rob. Appreciate the response. I follow what you are both
saying and I understand. I’m glad there is no name clash here as I have
run into that previously, but that was too obvious - the table was named
request! (I was just getting started.)

Anyway, I’m still somewhat confused as I could have swore I read in one
of my books that all the field values on the form are returned and
available to the controller, automatically done for you and each field
then available to you through params[:some_name]. Did I misunderstand or
read it wrong?

This would be similar to a form bean, though it did take a call to make
it happen, whereas I thought it was automatic in Rails.

If I understood that correctly, then is it different for this scenario
because it is a list?

(Of course I am trying to locate where I got the idea from but so far
have not been able to find it in the book.

Now, I am using partials with a layout template, and it just occurred to
me that I do not have a start_form_tag(:action => …") in my partial
form.

I hope this is making sense as I’m trying to deal here with a concept I
thought existed but cannot find it at the moment.

Bill W. wrote:

Hi Scott,

Scott H. wrote:

Anyway, I’m still somewhat confused as I could
have swore I read in one of my books that all the
field values on the form are returned and available
to the controller, automatically done for you and
each field then available to you through
params[:some_name]. Did I misunderstand or
read it wrong?

Two related items seem to me to be at the root of your current problem,
one
of which you’ve already figured out. Forms are not pages, they’re
‘containers’ (that are part of a page) that start and end with explicit
tags. The second item is about ‘fields’. Your understanding above is
correct. I think the problem you’re having here is your understanding
of
the term ‘field’. Your view contained no fields in the form-sense of
the
word. A form field is an object like a text_field, a check_box, a
radio_button, etc. The user-entered values of those things will be
passed
back to the controller in the params hash when the form is submitted.

hth,
Bill

Thanks Bill. I get it now. I was confused and that cleared it up.

I just took a look now at my ‘new’ view and it does use the
start_form_tag. Makes perfect sense and ties back in to what Tom and Rob
said and I’ve got the big picture now with all three pieces.

Thanks guys. I needed all three pieces to understand this. Much
appreciated.

Hi Scott:

Scott H. wrote:

Thanks Bill. I get it now. I was confused and that cleared it up.

Glad to help!

I just took a look now at my ‘new’ view and it does use the
start_form_tag.

Don’t forget the end_form_tag. :wink:

Thanks guys. I needed all three pieces to understand this.
Much appreciated.

I’m sure I can safely speak for Tom and Rob too when I say… welcome
aboard!

Best regards,
Bill

Yes, welcome aboard Scott :slight_smile: I think you will find that this list is
very helpful in getting you running with Ruby On Rails.

Good luck,

Tom

On 7/7/06, Bill W. [email protected] wrote:

Don’t forget the end_form_tag. :wink:


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Tom D.

http://atomgiant.com
http://gifthat.com

Very much appreciate the welcome Bill and Tom; really do. It will take
some time, but I am hoping to be able to contribute with being of help
to others eventually as this is I think the greatest form of saying
thank you. But it will be a while. In the mean time, for this out of
work COBOLer/some Java, it means a lot to me to be able to get some help
like this. And so I will try to make my questions good, well documented
and understandable so that the question and the answers will be of
benefit to others as well as myself.

So again, sincerely, thank you for the help and the welcome. :slight_smile: It’s
good to be part of the RoR community.