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
Related
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) %>
Below I have the link-helpers for the actions edit and destroy. The first link (and all the others) is working perfectly but the second creates a weird url that doesn't work.
<%= link_to "Edit", edit_event_path(organizer_vanity_url: event.organizer.vanity_url, id: event.id) %>
<%= link_to 'Remove', event_path(organizer_vanity_url: event.organizer.vanity_url, id: event.id), method: :delete, data: { confirm: 'Are you sure?' } %>
This is from the routes.rb:
scope "organizer" do
scope ":organizer_vanity_url" do
scope "manage" do
resources :events
end
end
end
What is the difference between the delete link-helper and the others (as that's the only one that doesn't work)?
link_to - is GET-request-like helper (by default)
DELETE method is POST-like method
so, you passing post method to get helper and receive "weird url"
to solve this you have two options:
use button_to instead of link_to helper (first one is for post form submitting, by default)
use js to correctly handle your link.
This comes from Twitter Bootstrap modal rails delete button not working
How can I pass html code to show in Twitter Bootstrap modal? Here is the link
<%= link_to t('delete'), post, method: :delete, confirm: t('delete_this_question'), 'data-my-message' => raw(post.text), class: 'label' %>
post.text is HTML code. Now it shows link in bad format.
Thanks
I think the problem here is probably your use of "raw". If you use raw html inside your link_to helper it will probably mess up the link's quoting. For example you might get something like this:
world" ...>
And that would mess up your link tag. I'm not sure how the rest of your view fits together but I think you should just be able to drop the 'raw' method:
<%= link_to t('delete'), post, method: :delete, confirm: t('delete_this_question'), 'data-my-message' => post.text, class: 'label' %>
I want a link to update a resource, without using an HTML form.
Routes:
resources :users do
resources :friends
end
Rake routes:
user_friend GET /users/:user_id/friends/:id(.:format){:action=>"show", :controller=>"friends"}
PUT /users/:user_id/friends/:id(.:format){:action=>"update", :controller=>"friends"}
I want to use the put to update a friend by a simple link, something like this:
<%= link_to "Add as friend", user_friend_path(current_user, :method=>'put') %>
But when I click the link, it tries to go into the show action.
What is the right way to do this?
link_to "Add as friend", user_friend_path(current_user, #friend), :method=> :put
Will insert a link with attribute 'data-method' set to 'put', which will in turn be picked up by the rails javascript and turned into a form behind the scenes... I guess that's what you want.
You should consider using :post, since you are creating a new link between the two users, not updating it, it seems.
The problem is that you're specifying the method as a URL query param instead of as an option to the link_to method.
Here's one way that you can achieve what you're looking for:
<%= link_to "Add as friend", user_friend_path(current_user, friend), method: 'put' %>
# or more simply:
<%= link_to "Add as friend", [current_user, friend], method: 'put' %>
Another way of using the link_to helper to update model attributes is by passing query params. For example:
<%= link_to "Accept friend request", friend_request_path(friend_request, friend_request: { status: 'accepted' }), method: 'patch' %>
# or more simply:
<%= link_to "Accept friend request", [friend_request, { friend_request: { status: 'accepted' }}], method: 'patch' %>
That would make a request like this:
Started PATCH "/friend_requests/123?friend_request%5Bstatus%5D=accepted"
Processing by FriendRequestsController#update as
Parameters: {"friend_request"=>{"status"=>"accepted"}, "id"=>"123"}
Which you could handle in a controller action like this:
def update
#friend_request = current_user.friend_requests.find(params[:id])
#friend_request.update(params.require(:friend_request).permit(:status))
redirect_to friend_requests_path
end
I use this code to delete record:
<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>
When I click the "Destroy", it will have an alert box prompt out and ask me to destroy or not. Can I modify the :confirm to an AJAX call? or can I add a :myConfirm in to the link_to tag to implement my own :myConfirm? Thx.
Indeed you can. Create your own helper method (suggested) or extend the default link_to helper (not suggested) in order to include your custom behavior.
If you are wondering whether you can achieve the result simply passing an additional option, the answer is no.
You actually need to code the AJAX call or the feature, then create the helper logic to handle the new behavior.
Best of all, avoid hacking the helper itself and create an unobtrusive JavaScript callback based, for instance, on a specific tag class.
You can easily to this in jQuery
$("ajax-confirm").click(function(){
// here send the AJAX request
// and display the confirm dialog
var confirm = true;
return confirm;
});
<%= link_to "...", "...", :class => "ajax-confirm" %>