0

Here is my problem. I want to pass the value from number_field_tag to the rails controller through link_to. here is a view:

<%= number_field_tag :quantity %>
<% link_to "do something", :controller => 'somecontroller', :action => 'dosomething', :pass_value_from_here => 'quantity' %>

and

controller:

def dosomething
   @ivar = Model.new(:quantity => :value_passed_from_link_to )
   # do somthing
end

Plus, I am not trying to use form. I have some other params too to be passed through link_to. for e.g. <%link_to "do something", :controller => "somecontroller", :action=>'dosomething', :pass_value_from_text_field => 'quantity', :some_other_value_from_some_other_model %> thank you in advance.

SujitS
  • 11,063
  • 3
  • 19
  • 41
  • Check out this question: http://stackoverflow.com/questions/11645153/submit-form-using-link-to-in-rails – Bart Dec 26 '13 at 18:41

1 Answers1

0

You probably don't want to use link_to here, you'll want to create a form which submits to your somecontroller#dosomething method.

You'll then find params[:quantity] populated with the input provided.

Something like this:

<%= form_tag somecontroller_dosomething_path do %>
  <%= text_field_tag :quantity, params[:quantity] %>
  <%= submit_tag :quantity %>
<% end %>
Donovan
  • 15,917
  • 4
  • 22
  • 34
  • I know I can do it, but here the case is different. I have some other params too from some other model to be passed. so, I dont think submit and form will help me right now. – SujitS Dec 26 '13 at 21:53
  • AFAIK, It's the only way to obtain a variable from your user without using a specific `GET` request or Javascript. – Donovan Dec 26 '13 at 21:56
  • Answered in my previous comment. What you're talking about is called `binding`, and you'll need to use Javascript for that. – Donovan Dec 26 '13 at 21:58
  • isn't there any other method to pass the input parameter through url to the controller. – SujitS Dec 26 '13 at 22:01
  • Yes, via a `GET` request, but you have to _know what the value of `quantity` is first_ in order to do that. You're trying to put the cart before the horse here. – Donovan Dec 26 '13 at 22:03
  • Would you please explain it little further. Perhaps with a example. – SujitS Dec 26 '13 at 22:04
  • A `GET` Request is something like `http://myserver/mypage?quantity=0` You have to know the value of `:quantity` in order to send that. My understanding is you are trying to obtain that value from your users. – Donovan Dec 26 '13 at 22:07
  • No, :quantity is user input and can be any thing. – SujitS Dec 26 '13 at 22:09
  • Okay, so how would you send it via a `GET`? You cannot -- unless you ask your user to specifically type it in the URL. You cannot take it from an unsubmitted text_input_field and place it there. It doesn't work that way. – Donovan Dec 26 '13 at 22:10