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>
Related
My method does not work.
I have a games library, where I can add, show, edit, and destroy. I want to expand it and add one more method clone to it.
Library created with rails g scaffold:
The clone method in games_controller.rb
def clone
#game.clone
respond_to do |format|
format.html { redirect_to games_url, notice: 'Game was successfully cloned.'}
format.json { head :no_content }
end
end
The call code for Clone in index
<% #games.each do |game| %>
<tr>
<td><%= game.title %></td>
<td><%= game.description %></td>
<td><%= link_to 'Show', game %></td>
<td><%= link_to 'Edit', edit_game_path(game) %></td>
<td><%= link_to 'Destroy', game, method: :delete, data: { confirm: '.. sure?' } %></td>
<td><%= link_to 'Clone', game, method: :clone, data: { confirm: '.. sure?' } %></td>
</tr>
<% end %>
The routes code in routes.rb
resources :games do
resources :comments
post 'clone'
end
a view in games library
You are doing it wrong. You have method: :clone which is not valid. The valid values for method option for link_to are post, patch, put and delete.
method: symbol of HTTP verb - This modifier will dynamically create an
HTML form and immediately submit the form for processing using the
HTTP verb specified. Useful for having links perform a POST operation
in dangerous actions like deleting a record (which search bots can
follow while spidering your site). Supported verbs are :post, :delete,
:patch, and :put. Note that if the user has JavaScript disabled, the
request will fall back to using GET. If href: '#' is used and the user
has JavaScript disabled clicking the link will have no effect. If you
are relying on the POST behavior, you should check for it in your
controller's action by using the request object's methods for post?,
delete?, patch?, or put?.
Now coming to your routes, when you do rake routes, you will see the below
game_clone POST /games/:game_id/clone(.:format) games#clone
So, the link_to should look like below
<%= link_to 'Clone', game_clone_path(game), method: :post, data: { confirm: '.. sure?' } %>
Couldn't find Game without an ID
Ok, you are cloning a Game instance. Normally you look for an instance with an :id, but with your current routes, the :game_id will passed in the params which is not appropriate to want you need. You should change your routes to pass :id instead of :game_id. So the final solution will be
Final Solution:
resources :games do
resources :comments
post 'clone', on: :member
end
The above will create a path helper with the below
clone_game POST /games/:id/clone(.:format) games#clone
Now change the link_to with the new path helper and you are good to go
<%= link_to 'Clone', clone_game_path(game), method: :post, data: { confirm: '.. sure?' } %>
Note:
You should be careful when adding custom routes to your resourceful routes. This will create path helpers with unnecessary keys such as :*_id(:game_id in your case). These routes should be added as collection route or a member route depending upon the scenario. For more info refer these guides
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.
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?' }%>
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.