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.
Related
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].
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?' }%>
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
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>
when i use method 'destroy' show me an error
Couldn't find Post with 'id'=#<Post::ActiveRecord_Relation:0x000000045e14c0>
method destroy
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
view
<%= link_to 'Destroy', post_path(#posts),
method: :delete,
data: { confirm: 'Are you sure?' } %>
sorry for my bad English
You have #posts variable defined as a ActiveRecord::Relation (some kind of models array, defined with #posts = Post.all or anything like that). To fix your issue, if you have link_to call inside index url, most probably you are doing something like:
<% #posts.each do |post| %>
# link_to call somewhere here
<% end %>
Then you have to change your link_to call to
<%= link_to 'Destroy', post_path(post),
method: :delete,
data: { confirm: 'Are you sure?' } %>
Notice we use variable defined in .each do |var| block, here it's post