Rails 2.3.5: Trouble passing parameters in a link_to tag

28 views
Skip to first unread message

Ian Baraza Brayoni

unread,
Aug 10, 2014, 10:44:01 PM8/10/14
to sdr...@googlegroups.com
I am trying to pick rails and I am having trouble trying to pass params from a view to another controller method through a link_to tag.

Here is my code.

Your ideas are highly appreciated.

Thank you.

Cynthia Kiser

unread,
Aug 11, 2014, 12:31:40 AM8/11/14
to sdr...@googlegroups.com
Quoting Ian Baraza Brayoni <ibara...@gmail.com>:
> I am trying to pick rails and I am having trouble trying to pass params
> from a view to another controller method through a link_to tag.
>
> Here is my code <https://gist.github.com/Brayoni/3102c5dab7f76b1cee7b>.
>
> Your ideas are highly appreciated.

Nothing very obviously amiss - other than being very old style
Rails. What is the output? Are you not getting the HTML you expect
from the link_to? Or is clicking the link not getting you where you
expect to go? To help I would need to see the HTML snippet produced
and the development log from when you click the link. (And I am
assuming your routes are set up correctly).



--
Cynthia N. Kiser
c...@ugcs.caltech.edu

Ian Baraza Brayoni

unread,
Aug 11, 2014, 12:57:36 AM8/11/14
to sdr...@googlegroups.com
This is what I get in my development logs which I think means I am not passing any parameters to the controller 'show_indisciplines':

ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each) on line #36 of app/views/employee_indisciplines/show_indisciplines.html.erb:
33:             <tr class="tr-head">
34:                 <td><%= t('student_case_content') %></td><td><%= t('student_case_creation') %></td>
35:             </tr>
36:             <% @employee_indiscipline.each do |employee | %>
37:                 <tr class="tr-<%= cycle('odd', 'even') %>">
38:                     <td class="col-4"><%= employee.content %></td>
39:                     <td class="col-7"><%= employee.created_at.strftime("%A, %d. %B %Y, %H:%M") %></td>


I have shared my routes on the gist.(https://gist.github.com/Brayoni/3102c5dab7f76b1cee7b)

Regards,
Ian Brayoni

       





--
--
SD Ruby mailing list
sdr...@googlegroups.com
http://groups.google.com/group/sdruby
---
You received this message because you are subscribed to a topic in the Google Groups "SD Ruby" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sdruby/Wy-Zldi3hRM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sdruby+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cynthia Kiser

unread,
Aug 11, 2014, 11:57:34 AM8/11/14
to sdr...@googlegroups.com
You asked about passing parameters so, in addition to the code
generating the link, I need to see the link that it creates and how
the routing system interprets it.

Please add the following 2 pieces of information to your gist:
1) the rendered html from _list_indisciplines.html.erb
2) the section of the development log when you click that link, e.g.

Processing PagesController#show (for 131.215.229.27 at 2014-03-12 10:46:17) [GET]
Parameters: {"action"=>"show", "url_parts"=>["protocols"], "controller"=>"pages"}


And by the way, since you have map.feed, that should be creating a
named route for you: feed_path. So if you want, you can use that
instead of the hash explicitly listing controller and action. Use
`rake routes` to see which of your routes have named route helper
methods available.
> You received this message because you are subscribed to the Google Groups "SD Ruby" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sdruby+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Rob Kaufman

unread,
Aug 11, 2014, 6:53:33 PM8/11/14
to Cynthia Kiser, sdr...@googlegroups.com
Ian,
  From reading your gist, I think you’re talking about the link_to on line 17 of “_list_indisciplines.html.erb”.  You’ve got a period in your params

employee.id is not valid (you’re setting to the id (4), not to the symbol itself).  Also, params[:retrieve_case[]] means that you’re getting [] every time.

<%= link_to "#{t('retrieve_case')}", {:controller => 'employee_indisciplines', :action => 'show_indisciplines', :employee.id => params[:retrieve_case[]]}, { :class => 'submit_button' }, :method => 'post' %>

Ian Baraza Brayoni

unread,
Aug 12, 2014, 1:06:47 AM8/12/14
to sdr...@googlegroups.com, Cynthia Kiser, rgka...@gmail.com
Hi Cynthia/Kaufman,

Thank you for your response.

I have edited my gist and it looks like, this line:

<%= link_to "Retrieve Case", {:controller => 'employee_indisciplines', :action => 'show_indisciplines', :employee_id => @retrieve_case.inspect }, { :class => 'submit_button' }, :method => 'post' %>

is receiving a nil id after doing an inspect on it( I have shared my development logs in the gist).

1234567
Processing EmployeeIndisciplinesController#show_indisciplines (for 127.0.0.1 at 2014-08-12 07:27:35) [GET]
Parameters: {"action"=>"show_indisciplines", "employee_id"=>"nil", "controller"=>"employee_indisciplines"}

as a result, the following section in my rendered html is iterating on a nil object hence nothing is displayed.

<% @employee_indiscipline.each do |employee | %>
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-4"><%= employee.content %></td>
<td class="col-7"><%= employee.created_at.strftime("%A, %d. %B %Y, %H:%M") %></td>
</tr>
<% end %>


Regards,
Ian Brayoni

       


Adam Grant

unread,
Aug 12, 2014, 1:28:33 PM8/12/14
to sdr...@googlegroups.com
The only thing I could think of is the "inspect" call:
  
   :employee_id => @retrieve_case.inspect

Inspect usually returns a string of some nicely formatted, human readable representation of the object (see this, and click the "Show Source" link at the bottom to see what it returns: http://apidock.com/rails/ActiveRecord/Base/inspect)

What exactly is in the @retrieve_case object? You may want to do @retrieve_case directly. The link_to should be able to stringify the object for you.

- Adam

Rob Kaufman

unread,
Aug 12, 2014, 2:19:24 PM8/12/14
to Adam Grant, sdr...@googlegroups.com
Hey Ian,
  You’re inpsect is telling us that @retrieve_case isn’t populated.  You are populating that directly from the params, are there ids being passed in on the URL? The problem you have now is from before you render this view/controller.

Rob
Reply all
Reply to author
Forward
0 new messages