Trying to delete an item from index.html.erb with following code:
<td><%= link_to 'Delete', method: :delete, id: product.id, data: { confirm: 'Are you sure?' } %></td>
however all it does is try to show the index again. How can I be more specific, I.e. - if this is a product from a category, how to I reference the path to this object in the database to delete it?
Define your action for delete and implement deleting a product in there.
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].
I want to delete a record that is stored in a table using a link right next to the table data. The error I come up with is:
No route matches [GET] "/carlogs/destroy"
My destroy method:
def destroy
#carlog= CarLog.find(params[:id])
#carlog.destroy()
redirect_to show_path(#carlog)
end
Part of the view code containing the Delete link:
<% #carlogs.each do |car| %>
<tr>
<td><%= car.id %></td>
<td><%= car.plate_number %></td>
<td><%= car.brand %></td>
<td><%= car.slot_number %></td>
<td><%= car.is_taken %></td>
<td><%= car.created_at %></td>
<td><%= link_to "Delete", show_path(car), method: :delete, data:
{confirm: "Are you sure?"} %>
</tr>
<% end %>
Make sure that you have delete REST method as:
DELETE /carlogs/:id(.:format) carlogs#destroy
And in your application.js you must have this line:
//= require jquery_ujs
Use destroy link for destroy record then redirect table like
method
def destroy
#carlog= CarLog.find(params[:id])
#carlog.destroy()
redirect_to show_path(#carlog) #-> Table pathe
end
routes.rb
delete 'destroy' => 'carlogs#destroy'
View
<%= link_to "Delete", destroy_path, method: :delete, data:
{confirm: "Are you sure?"} %>
I think will help you
Why did you write show_path(car)?
Maybe you mean car_path(car) ?
<%= link_to "Delete", car_path(car), method: :delete, data:
{confirm: "Are you sure?"}%>
Also you should check your route [GET] "/carlogs/destroy.
I think this don't present in rake router
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>
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>
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.