Sending parameters to controller using link_to but getting the variable name itself.

1,479 views
Skip to first unread message

Padmahas Bn

unread,
Jun 22, 2016, 4:20:22 AM6/22/16
to Ruby on Rails: Talk
Hi,

I'm not using any form in this web page. I only have labels, text boxes and links with parameters.
The text_field code is
<div class="field">
                   
<%= label_tag("Member code") %>
                   
<%= text_field_tag :member_code, nil, class: 'form-control'    %>
</div>

The link_to code is

<%= link_to "Joint and Personal loan (Ajax not implemented)", jlpl_prints_path(member_code: 'member_code'), :class => "btn btn-danger", method: :post %>

in the controller I printed member_code to verify whats the value I'm getting.
member_code = params[:member_code]
puts
"***********Inside controller member_code received is #{member_code} **************"

The thing is its printing the output in the console as
**************Inside controller member_code received is member_code******************

Why is that? I've filled text_field with value 180 but I'm getting the variable itself but not the value of it. Is form_tag is necessary to get this to work?

Thank you.

nanaya

unread,
Jun 22, 2016, 4:25:46 AM6/22/16
to rubyonra...@googlegroups.com
Hi,

On Wed, Jun 22, 2016, at 17:20, Padmahas Bn wrote:
> <%= link_to "Joint and Personal loan (Ajax not implemented)",
> jlpl_prints_path(member_code: 'member_code'), :class => "btn btn-danger",
> method: :post %>
>

The path includes a `member_code: 'member_code'` parameter and thus
passed to the controller.

Padmahas Bn

unread,
Jun 22, 2016, 4:39:57 AM6/22/16
to Ruby on Rails: Talk

How else can I send member_code to controller ?

What I meant in my first post is, I want to send member_code to controller, But when I check the value of member_code variable in the controller, it doesn't contain any value but the variable itself. As I told before, I've filled my text_field with the member code of 180. When I print member_code in controller I should get 180 right? But I'm getting member_code itself. The output should be

**********Inside controller member_code received is 180****************

nanaya

unread,
Jun 22, 2016, 4:50:19 AM6/22/16
to rubyonra...@googlegroups.com
Hi,

On Wed, Jun 22, 2016, at 17:39, Padmahas Bn wrote:
> But when I check the value of member_code variable in the controller, it
> doesn't contain any value but the variable itself. As I told before, I've
> filled my text_field with the member code of 180. When I print
> member_code
> in controller I should get 180 right? But I'm getting member_code itself.
> The output should be

I missed the last part of your question, yeah it must be inside a form
tag. You can't tell browser to pass a random text box value without it
being inside a form tag or through javascript.

What you're doing now is just passing `member_code` value to
`member_code` parameter through query string
(`/path?member_code=member_code`), completely ignoring the text box.

Padmahas Bn

unread,
Jun 22, 2016, 5:12:46 AM6/22/16
to Ruby on Rails: Talk
I missed the last part of your question, yeah it must be inside a form
tag. You can't tell browser to pass a random text box value without it
being inside a form tag or through javascript.

What you're doing now is just passing `member_code` value to
`member_code` parameter through query string
(`/path?member_code=member_code`), completely ignoring the text box.

Hello nanaya need some more help. In this web page I've member_code text_field and that value (membercode) has to be passed to different controllers which is done by more than one link_to helpers. Hence I can't provide a single url_option to form_tag. So I googled "form_tag without action" and found a way to write it.
<%= form_tag "#" do %>
     
<%= link_to "joint_loan", joint_loan_path(member_code: membercode), method: :post %>
     
<%= link_to "payment_detail", ............................ %>
     
<%= link_to "..................................................... %>
<% end %>



But still I'm not able to get the value of member_code.

Thank you.

nanaya

unread,
Jun 22, 2016, 5:15:13 AM6/22/16
to rubyonra...@googlegroups.com
Hi,

On Wed, Jun 22, 2016, at 18:12, Padmahas Bn wrote:
> Hello nanaya need some more help. In this web page I've member_code
> text_field and that value (membercode) has to be passed to different
> controllers which is done by more than one link_to helpers. Hence I can't
> provide a single url_option to form_tag. So I googled "form_tag without
> action" and found a way to write it.
> <%= form_tag "#" do %>
> <%= link_to "joint_loan", joint_loan_path(member_code: membercode),
> method: :post %>
> <%= link_to "payment_detail", ............................ %>
> <%= link_to ".....................................................
> %>
> <% end %>
>
>
>
> But still I'm not able to get the value of member_code.
>

Try using button. Or learn some more html (and javascript).

Rob Biedenharn

unread,
Jun 22, 2016, 10:40:22 AM6/22/16
to rubyonra...@googlegroups.com
On 2016-Jun-22, at 04:20 , Padmahas Bn <padm...@gmail.com> wrote:

Hi,

I'm not using any form in this web page. I only have labels, text boxes and links with parameters.
The text_field code is
<div class="field">
                    <%= label_tag("Member code") %>
                    <%= text_field_tag :member_code, nil, class: 'form-control'    %>
</div>

The link_to code is

<%= link_to "Joint and Personal loan (Ajax not implemented)", jlpl_prints_path(member_code: 'member_code'), :class => "btn btn-danger", method: :post %>

You want to remove the quotes from the value:
member_code: 'member_code'
and just have:
member_code: member_code

However, you will have a form in your page due to the use of method: :post in your link_to; it's just a generated form.

As others have said, you'll need to do some Javascript (perhaps with a library like jQuery) to get the value of a field at the time the link is clicked without using a form.

-Rob

in the controller I printed member_code to verify whats the value I'm getting.
member_code = params[:member_code]
puts"***********Inside controller member_code received is #{member_code} **************"

The thing is its printing the output in the console as
**************Inside controller member_code received is member_code******************

Why is that? I've filled text_field with value 180 but I'm getting the variable itself but not the value of it. Is form_tag is necessary to get this to work?

Thank you.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3e05f0ea-f2f7-401f-b614-0a39a6920788%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages