Rails 2.3.5: Trouble passing params to controller method through link_to tag

89 views
Skip to first unread message

Ian Brayoni

unread,
Aug 10, 2014, 10:33:19 PM8/10/14
to rubyonra...@googlegroups.com
Hi guys,

I have been trying to pick up rails and I am having trouble passing
parameters through a link_to tag so as to retrieve records where there
is a parameter match.

https://gist.github.com/Brayoni/3102c5dab7f76b1cee7b

Here is my view;
<% @employees.each do |employee| %>

<div class="name_list<%=cycle('odd', 'even')%>"><li>
<label><%= check_box_tag "retrieve_case[employee_ids]",
employee.id, false,:class=>'right' %>
<div class="att_list_names"><%= employee.first_name
%></div> </label>
</li> </div>
<% end %>
</div>

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


controller method;
def show_indisciplines
if request.post?
activated_ids = params[:retrieve_case][:employee_ids].collect
{|employee_id| employee_id.to_i}

if activated_ids
@employee_indiscipline =
EmployeeIndiscipline.find_all_by_employee_id(params[:employee_id])
end
end
end

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

Frederick Cheung

unread,
Aug 11, 2014, 4:49:31 AM8/11/14
to rubyonra...@googlegroups.com


On Monday, August 11, 2014 3:33:19 AM UTC+1, Ruby-Forum.com User wrote:
Hi guys,

I have been trying to pick up rails and I am having trouble passing
parameters through a link_to tag so as to retrieve records where there
is a parameter match.

If you're trying to get the result of all those checkboxes why not use a form?

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

 
That :employee.id  isn't right, but changing that won't help you as far as getting the data from the checkboxes

Fred

Javix

unread,
Aug 11, 2014, 9:13:09 AM8/11/14
to rubyonra...@googlegroups.com
First: you should not use params method in your views, it is only available in a controller as a Hash to get values from views.
Second: you  should just use a symbol to indicated the key to are going to pass from the view to the controller; I mean do not use :employee.id but :employee_id.
Third: as for Rails 2.3.8 API, you can specify link_to as follows:

<%= link_to "#{t('retrieve_case')}", {:controller => 'employee_indisciplines', :action => 'show_indisciplines',
:employee_id => retrieve_case, { :class => 'submit_button' }, :method => 'post' %>



You should replace retrieve_case by the appropriate value that is available in the veiw. In your controller you will get this value from the 'params hash:

patams[:employee_id]

Fourth: do not forget to change the routes (routes.rb file) to enable the called controller action.

Matt Jones

unread,
Aug 11, 2014, 11:46:37 AM8/11/14
to rubyonra...@googlegroups.com
This is not true. `params` is available in the view. It's not always a good idea to *use* it there, but it's available.


Relating to the original question, the difficulty appears to be that the link isn't going to pick up the client-side checkboxes. This code:

:employee.id => params[:retrieve_case[]]}, { :class => 'submit_button' }, :method => 'post'

doesn't make any sense.

I'd recommend getting this form to work with a real submit button and without AJAX *before* complicating it.

Also: 2.3.5 is deeply, wildly out-of-date. At a minimum, you should upgrade to the last security release of 2.3.x as there are SEVERE issues in older versions.

--Matt Jones

Ian Brayoni

unread,
Aug 12, 2014, 1:10:52 AM8/12/14
to rubyonra...@googlegroups.com
Thank you for your responses.

I have edited my
gist(https://gist.github.com/Brayoni/3102c5dab7f76b1cee7b) 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).

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 %>

Javix

unread,
Aug 12, 2014, 3:25:58 AM8/12/14
to rubyonra...@googlegroups.com


On Monday, August 11, 2014 4:33:19 AM UTC+2, Ruby-Forum.com User wrote:
You reference the variable in your view

:employee_id => @retrieve_case.inspect

but it is not initialised yet, because you will get it only in your controller

Dave Castellano

unread,
Aug 16, 2014, 4:41:50 PM8/16/14
to rubyonra...@googlegroups.com
Matt Jones wrote in post #1154884:
> On Monday, 11 August 2014 09:13:09 UTC-4, Javix wrote:
>>>
>>> </li> </div>
>>> def show_indisciplines
>>>
>>> --
>>> Posted via http://www.ruby-forum.com/.
>>>
>>
>> *First*: you should not use *params *method in your views, it is only
>> available in a controller as a Hash to get values from views.
>>
>
As a real novice, may I ask why it is not a good idea to use params in
the view ?

Thanks

Dave Castellano

Javix

unread,
Aug 17, 2014, 4:36:19 AM8/17/14
to rubyonra...@googlegroups.com


On Monday, August 11, 2014 4:33:19 AM UTC+2, Ruby-Forum.com User wrote:
because that's how the MVC pattern works, you passing values (via params hash) from your view to your controller which extracts the needed parameters values, processes them by making call to your model if needed, initialises/changes instance variables and passes them again to the view and displays them. Why to use params hash in a view if you can pass in the variables from the controller ? Doing like that, you are 'breaking' in some way the described pattern as well as Rails convention. Try to follow Rails guides (http://guides.rubyonrails.org) to have an idea about how it works and it will become more clear and easy for you.
Reply all
Reply to author
Forward
0 new messages