RESTful route add new post method - ruby-on-rails

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.

Related

How to Write link_to for acts_as_votable nested resources?

I am trying to add voting to my site. I have improvements that are created and displayed inside of the projects show page. I'm trying to allow users to vote on improvements but I am getting an error that I think is related to how I'm linking the like button.
in my routes.rb file:
resources :projects do
resources :improvements do
member do
put "like" => "improvements#upvote"
put "unlike" => "improvements#downvote"
end
end
end
In my view:
<%= link_to like_improvement_path(improvement), class: "like", method: :put do %>
Rails recommended me to write:
<%= link_to project_like_improvement_path(improvement), class: "like", method: :put do %>
But this doesn't work. So I tried doing this in my routes.rb:
resources :projects do
resources :improvements
end
resources :improvements do
member do
put "like" => "improvements#upvote"
put "unlike" => "improvements#downvote"
end
end
Using the original link_to, the voting works, but clicking on the vote button takes me to the improvements show page. I want to stay on the projects page.
If:
<%= link_to like_improvement_path(improvement), class: "like", method: :put do %>
Works then in the improvements controller's (upvote?) action simply do a:
redirect_to projects_path
At the end of whatever else you do model wise.
Change "projects_path" to the correct route for the page.
I had the same problem. The solution with your original routes.rb:
<%= link_to like_project_improvement_path(improvement.project, improvement), class: "like", method: :put do %>
You can write a redirect_to inside the upvote and downvote methods in the improvements controller.

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.

Simple Routing with Arrays

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.

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

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