Rails No route matches [DELETE] - ruby-on-rails

Trying to delete a reply
<% #comment.replies.each do |reply| %>
<td><%= link_to 'Destroy', comment_replies_path(reply), method: :delete, data: { confirm: 'Are you sure?' }%></td>
I do receive the following error
No route matches [DELETE] "/comments/66/replies"
rake routes
comment_reply_path
DELETE /comments/:comment_id/replies/:id(.:format) replies#destroy
The url that I am at this point is
http://localhost:3000/comments/66/replies
routes.rb
resources :comments do
resources :replies
end

Your route says comment_reply_path and you are using comment_replies_path
You need to update your code to use correct path helper
<%= link_to 'Destroy', comment_reply_path(reply), method: :delete, data: { confirm: 'Are you sure?' }%>

Related

Can we attach some variables to route path in ruby on rails 6?

I'm a beginner in ruby-on-rails and I struggled for hours to do this:
There is one variable X in a html page Page1 of one controller C1 in my application. And I want to link to anther route path path1 with that variable X so that I can do something with that variable X in the controller action act1 corresponding to that route path path1.
In my situation is:
<% #courses.each do |course| %>
<tr>
<td><%= link_to 'Enroll', enroll_path, method: :post, data: { confirm: 'Are you sure?' } %></td>
</tr>
This code is in the view page html and I want to link_to enroll_path with the variable course
post '/enroll', to: 'enrollments#enroll'
This is the enroll path route
def enroll
enrollment = Enrollment.new(user_id: current_user.id, course_id: course[id])
enrollment.save
redirect_to root_url
end
This is action that corresponds enroll path and I want use the variable course in it.
I have tried to attach the variable course directly to the enroll_path, Like this:
<td><%= link_to 'Enroll', enroll_path(course), method: :post, data: { confirm: 'Are you sure?' } %></td>
but it did not work out.
How should I do to solve this?
Have you tried
<%= link_to 'Enroll', enroll_path(course_id: course.id), method: :post, data: { confirm: 'Are you sure?' } %>
I believe you will have to change the controller line to this
enrollment = Enrollment.new(user_id: current_user.id, course_id: params[:course_id])
This part (course_id: course.id) sends course_id as a param, so in the controller you can access its value by looking for params[:course_id].

Rails Routing Error (undefined local variable or method `micropost_comment')

NameError in StaticPages#home
I don't quite understand why. Here my code from views/comments/_comment.html.erb
<% if current_user?(comment.user) %>
<%= link_to 'Destroy', micropost_comment, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
Here's my routes
micropost_comment GET /microposts/:micropost_id/comments/:id(.:format) comments#show
PATCH /microposts/:micropost_id/comments/:id(.:format) comments#update
PUT /microposts/:micropost_id/comments/:id(.:format) comments#update
DELETE /microposts/:micropost_id/comments/:id(.:format) comments#destroy
can some one explain how to fix this error
undefined local variable or method `micropost_comment'
thank you in advance
The second argument of link_to is a path
micropost_comment is not a path, you should replace it with micropost_comment_path
also regarding to you routes, you should add those params :
<%= link_to 'Destroy', micropost_comment_path(micropost_id:comment.micropost_id, id:comment.id), method: :delete, data: { confirm: 'Are you sure?' } %>
I think you don't need nesting routes though, but it's an other question

How to do delete link in rails with modules

So I am unsure of the syntax for the delete record link because I have modules for the routes
namespace :api do
namespace :v1 do
resources :invites do
collection do
post "by_user_id"
end
end
end
end
This is the syntax for the delete method right now
<td><%= link_to 'Destroy', invite, method: :delete, data: { confirm: 'Are you sure?' } %></td>
Also I am using rails 4
Please check routes via rake routes DELETE method for invites resource.
Then insert your routes below in place of "your_routes_path"
<td><%= link_to 'Destroy', your_routes_path, method: :delete, data: { confirm: 'Are you sure?' } %></td>

Ruby On Rails strange routes path

I have this routes in my app (rails 3.2):
godmode_invites GET /godmode/invites(.:format) godmode/invites#index
POST /godmode/invites(.:format) godmode/invites#create
new_godmode_invite GET /godmode/invites/new(.:format) godmode/invites#new
edit_godmode_invite GET /godmode/invites/:id/edit(.:format) godmode/invites#edit
godmode_invite GET /godmode/invites/:id(.:format) godmode/invites#show
PUT /godmode/invites/:id(.:format) godmode/invites#update
DELETE /godmode/invites/:id(.:format) godmode/invites#destroy
And in template view:
<td><%= link_to 'Show', godmode_invites_path(invite) %></td>
<td><%= link_to 'Destroy', godmode_invites_path(invite), method: :delete, data: { confirm: 'Are you sure?' } %></td>
This produces strange routes like with point before resource ID:
/godmode/invites.3
/godmode/invites.4
I can not find my problem...
There is little error your in view. Here is the corrected code:
<td><%= link_to 'Show', godmode_invite_path(invite) %></td>
<td><%= link_to 'Destroy', godmode_invite_path(invite), method: :delete, data: { confirm: 'Are you sure?' } %></td>

Link for destroy returns "undefined method"

I have the*Topics* controller and in the view/topics/index.html.erb the link for destroying item:
<%= link_to 'Destroy',topic, confirm: 'Are you sure?', method: :delete %>
I tried also
<%= link_to 'Destroy', topic_path(topic), confirm: 'Are you sure?', method: :delete %>
but both returns
undefined method topic_path for Class:0x00000105056a80>:
0x00000105047328>
in routes.rb is following:
namespace :admin do
...
resources :topics
end
Where could be the problem and how to in the easily way to solve it? I was checking the other generated controllers/views by CRUD, and the setup is always the same and in all other controllers it's working well, just in this one I am getting over and over again this error.
Try this:
<%= link_to 'Destroy', admin_topic_path(topic),
confirm: 'Are you sure?', method: :delete %>
To make sure run command rake routes, in the result you should see your route.

Resources