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.
Related
I have the following list of items:
= #kid.educations.each do |education|
= education.studies_centre
= _('-')
= education.city_and_country
= link_to _("<i class='fa fa-times-circle a-lg'></i> delete").html_safe, education, :confirm => 'Are you sure?',:method => :delete, class: "btn btn-danger btn-xs pull-right"
%br
%small
= education.academic_qualification
%hr
%br
As you can see, I have a delete option, but I does not work. I get:
undefined method `education_path' for #<#<Class:0x007fd00c61c270>:0x007fd00cdd5b88>
What I'm doing wrong.
Thanks for your help
UPDATE ROUTES
dashboard_kid_educations GET /dashboard/kids/:kid_id/educations(.:format) dashboard/educations#index
POST /dashboard/kids/:kid_id/educations(.:format) dashboard/educations#create
new_dashboard_kid_education GET /dashboard/kids/:kid_id/educations/new(.:format) dashboard/educations#new
edit_dashboard_kid_education GET /dashboard/kids/:kid_id/educations/:id/edit(.:format) dashboard/educations#edit
dashboard_kid_education GET /dashboard/kids/:kid_id/educations/:id(.:format) dashboard/educations#show
PUT /dashboard/kids/:kid_id/educations/:id(.:format) dashboard/educations#update
DELETE /dashboard/kids/:kid_id/educations/:id(.:format) dashboard/educations#destroy
You're using namespaced nested resources, so your link should be like this:
= link_to _("<i class='fa fa-times-circle a-lg'></i> delete").html_safe, [:dashboard, #kid, education], :confirm => 'Are you sure?',:method => :delete, class: "btn btn-danger btn-xs pull-right"
If you using nested resources you should pass 2 arguments to path helper. And I think it's better to use block to improve code readability
= link_to dashboard_kid_education_path(#kid, education), confirm: 'Are you sure?', method: :delete, class: 'btn btn-danger btn-xs pull-right' do
%i.fa.fa-times-circle.a-lg
= 'delete'
Nested Routes
Something to add to Marek's answer is the idea of nested routes
Essentially, when you define a nested route (like below), Rails does not instinctively know when you're calling this:
#config/routes.rb
resources :dashboard do
resources :education
end
The bottom line is that Rails will only call routes as they pertain to the model of the object you're calling. In your example, you call education (which is presumably built from Eduction.find())
To Rails, by passing this to your routes, you will essentially tell your application to find routes for the Education model only, hence why your error looks like this:
undefined method `education_path'
--
The way to fix this, as detailed by Marek is to ensure you are referencing the correct path in your <%= link_to %> call. To do this, you have to reference the nested route as described by Marek's solution
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.
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.
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 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 %>