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

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

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

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.

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

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

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

Matt Jones wrote in post #1154884:

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.