I'm creating a online order system for a restaurant using rails and i want to be able to attach modifiers to items using ajax i have
<%= link_to(mod.name , add_modifier_url , method: :put, remote: true, mod_id: mod.id, item_id: item.id) %>
the call is making it to my controller but i cant access any of the params I'm sending how do you pass params with link_to and ajax in rails
thanks
EDIT
I think I answered my own question, I just had to put the params i wanted to send as params for the url
<%= link_to(mod.name , add_modifier_url( mod_id: mod.id, item_id: item.id) , method: :put, remote: true) %>
Related
I want to call an api endpoint on my rails app.
I cannot find the way to pass post params from link_to nor from form_tag
link_to '/v1/my_endpoint/approve', data: { confirm: "Are you sure?" }, remote: true, method: :post, id: 'approve-id' do 'link_name' end
I want together with the above link to pass some post params.
link_to isn't going to do what you need. You could pass query params that way, but not form params.
You need to use a form, or write some javascript to submit the request for you.
Rails has a handy button_to helper which will create a small form, presented in your UI as a single button. You can add params to that quite easily:
<%= button_to "button label", "/v1/my_endpoint/approve", remote: true, params: { id: "approve-id", something: "else" } %>
If you wanted, you could use CSS to style that button as a link.
You can read about the button_to method in the docs here:
https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
As I understand it, Ajax allows you to only refresh the part of the page that you specify, as opposed to reloading the whole thing. Is there a way of doing this with a link_to method, for example, <%= link_to 'approve', approve_model_path(#model.id), method: :put %> ? I can't figure out how a link_to could be executed without the whole page being reloaded.
You can apply ajax by setting remote to true:
<%= link_to 'approve', approve_model_path(#model.id), method: :put, remote: true %>
This does a js request, then you need an approve.js.erb to do the few changes you need (without refreshing the page)
I have the following form for posting data using Ajax.
<%= form_tag(check_answer_path, :id =>'myForm', :method => 'post', :remote => true) do %>
some form fields here...
<%= submit_tag('Next', :class => 'btn btn-primary') %></td>
But the form does not submit when I click on the Next button. To my surprise when I remove the :id and :remote attributes the form submits with no problem(but the page reloads which i do not want).
GOAL: I want to submit the form using ajaxForm() to handle the call back i.e $("#myForm").ajaxForm(). So I need the :remote attribute and I also need to identify my form(using the :id attribute).
Why is my form not submitting when I include :id and :remote attributes?
Am using rails 3.2.8 and jquery-rails.
Thanks in advance.
Adding remote to the form tag means that it's going to send the request using JS.
So it's actually submitting the form but it's not submitting it in the expected way.
In your controller add the following:
respond_to do |format|
format.js
format.html
end
and you will be able to respond to that request place a file.js.erb in your views and place the needed js code to complete the request.
Hope that helps.
If you have a post action to "do_something/:id" => :start (say with a named route 'start'), how do you create a form_tag that submits the :id based on a select_tag selection by the user ?
Assuming you aren't using resources.
The issue is that you have a post action, however you want to set a GET variable via a form submission(that has to use POST).
You can make the form make a GET request, like so:
<%= form_tag(start_path, :method => "get") do %>
<%= select_tag "id", "<option>1</option><option>2</option><option>3</option><option>4</option>" %>
<% end %>
I have a form which has this text field
<%= f.text_field :content %>
I have a link_to tag to post the value of the text field to an action present in another controller. I need to be able to get the value of the text field and say
<%= link_to 'post', :controller => "a_different_controller", :action => "update", :message => "text field's value" %>
Can you please help me out here?
I tried various options posted on stack overflow. None seem to work.
If you want to POST a value without a html form, you must do this via javascript. You can create a onClick listener for that link, and in the listener grab the value of that text_field and submit the form via javascript.
You can pass the remote and method options on your link_to helper:
<%= link_to 'Post', yourmodel_path, :remote => true, :method => :put %>
That will submit it via ajax. If you don't want that and you just want to use the PUT HTTP verb, you can drop the :remote => true portion.
Note that I'm using the shorthand way of referring to the action you are targeting. If you're not familiar with that, I suggest you run through the Rails Guides…it's pretty core to understanding example code.