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
Related
I want to show button with image.
I have this code
<%= image_submit_tag "down.png", controller: "posts", action: "votedown", post_id: post.id, topic_id: post.topic_id, class: "xta" %>
Its visible properly but not calling action "votedown"
In my routes I have
post '/votedown', to: 'posts#votedown
Please also suggest if there is any other way to call the method votedown with params and image "down.png"
image_submit_tag must be used in conjunction with a form - it works just a normal html <input type="submit"> button.
You might also want to change your route definition into something more restful:
patch '/posts/:id/votedown' => "posts#votedown", as: 'votedown_post'
This makes it more apparent that this route acts on a post - and we use the PATCH method since we are changing a resource instead of creating a new resource.
Armed with our new route we can simply create a form:
<%= form_for(#post, url: votedown_post_path(#post) ) do |f| %>
<%= image_submit_tag "down.png", class: "xta" %>
<% end %>
Note that you do not need to add an input for the post id since it will be available as params[:id].
Another way to do this would be to use Rails unobstructive javascript driver to create a link or button which sends a PATCH request to '/posts/:id/votedown'.
<%= link_to image_tag("down.png", class: "xta"), votedown_post_path(#post), method: :patch %>
I have this route:
delete 'basket/remove' => 'flowercard_baskorder#remove', as: :basket_remove
This link in view:
<%= link_to "Remove", basket_remove_path %>
And this in my controller (the binding is just for me to test):
def remove
binding.pry
end
When the link is clicked on nothing happens and i have no idea why!? I'm obviosuly expecting the binding to kick in but doesn't even look like a request is made?
Cheers
link_to defaults to a get request but you want it to go to a delete right? Just specify that in your call:
<%= link_to "Remove", basket_remove_path, method: :delete %>
You need to pass the method to the link_to function
<%= link_to "Remove", basket_remove_path, method: "delete" %>
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.
I have a Model with an attribute votes. I have a link in a view that needs to increment the value of votes - what is the best way to do this?
I am currently trying a link like:
<%= link_to 'Up', '#', :method => :voteup %>
and a voteup method in the model_controller but this isn't working.
I think the best way would be this:
In config/routes.rb:
resources :quotes do
member do
post :upvote
end
end
And your link:
<%= link_to 'Up', upvote_quote_path(#quote), :method => :post %>
Note that we use a POST request, which is more appropriate than a GET request when modifying a record.
:method is only supposed to be used to specify between POST, GET, DELETE, and PUT requests. Your second parameter of link_to should be the action you want to execute in your controller.
<%= link_to "Up", :action => :voteup %>
I am trying to add a new page to my RoR3 application that should display a delete confirmation of a user account. It should match the 'destroy' action in 'ROOT_RAILS/controllers/accounts_controller.rb'.
At this time my problem occurs on creating a "link_to" this page, but maybe I am wrong somewhere and my work is not completed yet.
So, what I made, is:
I created the 'ROOT_RAILS/views/accouns/delete.html.erb' file.
I updated the routes.rb like this:
resources :accounts do
collection do
get 'delete'
post 'delete'
end
end
I don't know the next steps, but now if I try to insert this code
<%= link_to 'Delete', delete_account_path(#current_account) %>
in my views, I will get this error:
undefined method `delete_account_path' for #<#<Class:0x00...>
What I have to do?
This "link_to" works, but, of course, doesn't make what I would like:
<%= link_to 'Delete', delete_users_accounts_path %>
Try the following:
config/routes.rb:
resources :accounts do
get :delete, :on => :member
end
In the view before the delete page:
<%= link_to 'Delete', delete_account_path(#current_account) %>
In the delete view(this will invoke the destroy method in your controller):
<%= link_to 'Delete', #current_account, :confirm => "Are you sure?", :method => :delete %>