Simple Routing with Arrays - ruby-on-rails

So if I want an edit link, I can do either of the following:
link_to 'Edit', edit_user_task_path(#user, #task)
link_to 'Edit', [:edit, #user, #task]
If I want to delete one however, I have to do:
link_to 'Delete', [#user, #task], method: :delete
Is it possible to make rails understand the following?
link_to 'Delete', [:delete, #user, #task]
It seems like it tries to go to the "delete_user_task_path", is there a shorter form for delete like there is the edit?

What you have to understand is that the array argument for the link_to method doesn't touch the request method (as far as I know).
As so, your suggestion would actually going to do a GET request to something like /user/:id/delete.
You could make this happen with something like
resources :user do
get :delete, on: :member
end
But that's not very RESTful, and I wouldn't recommend it.

Related

The difference between delete and the other routes

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.

RESTful route add new post method

My routes file looks like this one with my new restful post action:
resources :projects do
post 'addpartner'
end
And in my view:
<%= link_to '[Add]', project_addpartner_url(#project,partner) ,
confirm: 'Are you sure?',
method: :post %>
Now the problem is project_addpartner_url generates the path with the default formatting. For my case it is something like:
/projects/1/addpartner.16
But my expected formatting is something like:
/projects/1/addpartner/16
How can I achieve this?
Try use another route, like:
resources :projects do
member do
post 'addpartner'
end
end
Or, maybe:
resources :projects do
collection do
post 'addpartner'
end
end
It seems to me that your link is setup as a GET methods, that's why you get
/projects/1/addpartner.16
But the way you want is seems like GET
/projects/1/addpartner/16
So try changing your link as
<%= link_to '[Add]', project_addpartner_url(#project,partner) ,
confirm: 'Are you sure?',
method: :get %>
But normally add/update/delete should be POST methods.

what is the right 'rails' way to add a link_to a new custom method

We're adding a new method 'delete_stuff' to the WidgetsController of a scaffolded app.
in routes we added
match 'widget/delete_stuff/:id' => 'widgets#delete_stuff'
I CAN manually create html (GET) links like
My Custom Delete Stuff
But that's bad on so many levels (uses GET instead of DELETE, doesn't permit a CONFIRM dialog, isnt DRY, etc)
Problem is I can't figure out how to use the url helpers for a custom method... trying to do something like this:
<% link_to 'DeleteStuff', #widget, :confirm => 'Are you sure?', :method => :delete %>
But that just gets ignored when the html is rendered.
I'm clearly missing something fundamental on how to use link_to, any help will be appreciated!
Cheers,
JP
Looks like you're missing an equals sign at the beginning. It should read:
<%= link_to 'DeleteStuff', #widget, :confirm => 'Are you sure?', :method => :delete %>
to solve your routing and make your additional action a delete method try this;
in routes.rb
resources :widgets do
member do
delete 'delete_stuff'
end
end
First run rake routes to know what URL helpers are available to you. You may see a line beginning with:
delete_stuff_widget
You can then append path or url to get the name you should use in your views and controllers. I suspect your new link_to will look like:
link_to "DeleteStuff", delete_stuff_widget_path(#widget), :confirm => "Sure?", :method => :delete

Best way to update an ActiveRecord attribute from a 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 %>

Making a delete confirmation page using Ruby on Rails 3

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 %>

Resources