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

Fb663cdf19f6a5820a564ae40558c6cf?d=identicon&s=25 Ian Brayoni (i_brayoni)
on 2014-08-11 04:32
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 %>

<%= 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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-08-11 10:50
(Received via mailing list)
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
F7141e066d691d7f18765c1ea6389b18?d=identicon&s=25 Serguei Cambour (javix)
on 2014-08-11 15:14
(Received via mailing list)
On Monday, August 11, 2014 4:33:19 AM UTC+2, Ruby-Forum.com User wrote:
> <% @employees.each do |employee| %>
> <%= link_to "#{t('retrieve_case')}", {:controller =>
>
>       if activated_ids
>         @employee_indiscipline =
> EmployeeIndiscipline.find_all_by_employee_id(params[:employee_id])
>       end
>     end
>   end
>
> --
> 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.
*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 <http://api.rubyonrails.org/v2.3.8/>,
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.
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2014-08-11 17:48
(Received via mailing list)
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.
>

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
Fb663cdf19f6a5820a564ae40558c6cf?d=identicon&s=25 Ian Brayoni (i_brayoni)
on 2014-08-12 07:09
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 %>
F7141e066d691d7f18765c1ea6389b18?d=identicon&s=25 Serguei Cambour (javix)
on 2014-08-12 09:27
(Received via mailing list)
On Monday, August 11, 2014 4:33:19 AM UTC+2, Ruby-Forum.com User wrote:
> <% @employees.each do |employee| %>
> <%= link_to "#{t('retrieve_case')}", {:controller =>
>
>       if activated_ids
>         @employee_indiscipline =
> EmployeeIndiscipline.find_all_by_employee_id(params[:employee_id])
>       end
>     end
>   end
>
> --
> Posted via http://www.ruby-forum.com/.
>

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
Be2b7277cff8516117cbd7a33df7b03a?d=identicon&s=25 Dave Castellano (dcastellano1)
on 2014-08-16 22:41
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
F7141e066d691d7f18765c1ea6389b18?d=identicon&s=25 Serguei Cambour (javix)
on 2014-08-17 10:37
(Received via mailing list)
On Monday, August 11, 2014 4:33:19 AM UTC+2, Ruby-Forum.com User wrote:
> <% @employees.each do |employee| %>
> <%= link_to "#{t('retrieve_case')}", {:controller =>
>
>       if activated_ids
>         @employee_indiscipline =
> EmployeeIndiscipline.find_all_by_employee_id(params[:employee_id])
>       end
>     end
>   end
>
> --
> Posted via http://www.ruby-forum.com/.
>
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.