My route.rb:
map.resources :car_users
car_users_controller.rb:
class CarUsersController < ApplicationController
def index
#car_users = CarUsers.all
end
def show
end
def delete
redirect_to :car_users_path
end
end
I created a delete link in index view:
...
<% #car_users.each do |car_usr| %>
<tr>
<td>
link_to "DELETE IT", delete_car_user_path(car_usr.id)
</td>
</tr>
...
But I got the error:
undefined method `delete_car_user_path' for #<ActionView::Base:0x9d17b40>
Why?
(I am working with Rails v2.3.2)
Because delete_car_user_path isn't a route and I assume you haven't defined it anywhere. If you want a delete link use:
link_to "DELETE IT", car_user_path(car_usr), :method => :delete, :confirm => 'Are you sure ?'
For future reference use rake routes on the command line to get a list of routes.
I do remember an instructor on Lynda.com saying that for whatever reason Rails (I'm on 4.2) does not implement the DELETE route. I feel like there has to be a better way to do this but this worked for me (using RESTful routes):
<%= link_to "DELETE IT", "car_users/#{car_usr.id}/delete", method: :get %>
Related
Edited to include config/routes.rb file.
I'm working through the Jumpstartlab Blogger 2 tutorial and I've run into trouble trying to delete tags from a tag list. This is my first Rails project and I'm still trying to wrap my head around MVC and routing.
Here's the code from my Tags view:
<h1>All Tags</h1>
<ul id="tags">
<% #tags.each do |tag| %>
<li>
<%= link_to tag.name, tag_path(tag), class: 'tag_name' %>
<%= link_to "Delete", tags_path(#tag), method: :delete, data: {confirm: "Really delete the tag?"} %>
</li>
<% end %>
</ul>
And the code from my Tags controller:
class TagsController < ApplicationController
def show
#tag = Tag.find(params[:id])
end
def index
#tags = Tag.all
end
def destroy
#tag = Tag.find(params[:id])
#tag.destroy
end
end
And config.routes.rb:
Blogger::Application.routes.draw do
root to: 'articles#index'
resources :articles do
resources :comments
end
resources :tags
end
The error I'm getting is No route matches [DELETE] "/tags".
I feel like the issue is something basic that I haven't quite grasped yet. I would really appreciate if someone could help me understand what I've missed and how it works. If I haven't provided enough information, please let me know. And thanks!
It's because the DELETE route is based on an instance (just 1 tag) of a resource, not the collection (the group of tags).
So you have to change this line:
<%= link_to "Delete", tags_path(#tag), method: :delete, data: {confirm: "Really delete the tag?"} %>
To use tag_path(tag):
<%= link_to "Delete", tag_path(tag), method: :delete, data: {confirm: "Really delete the tag?"} %>
In my application I have a user, whose table in the database contains a boolean column for admin status. I want an admin to be able to change the admin status of any other user. The relevant view is:
<% #users.each do |user| %>
<tr>
...
<% if current_user && (current_user.id == user.id || current_user.admin) %>
...
<% if current_user.admin %>
<td><%= link_to 'Make admin', user.id.to_s + '/make_admin', {:method => 'post', :action => 'make_admin'} %></td>
<% if user.admin %>
<td><%= link_to 'Revoke admin', user.id.to_s + '/revoke_admin', {:method => 'post', :action => 'revoke_admin'} %></td>
<% end %>
...
</tr>
<% end %>
I wasn't sure how else to do it so I cobbled together the URL by concatenating the user's ID and the specific query. My controller begins with the restrictions which seem to prevent unauthorized access to these methods:
class UsersController < ApplicationController
...
before_action :admin_logged_in, only: [:edit, :update, :destroy, :make_admin, :revoke_admin]
And finally, my routing file looks like this:
Rails.application.routes.draw do
...
post ':id/make_admin' => 'users#make_admin'
post ':id/revoke_admin' => 'users#revoke_admin'
It seems to work, but I was wondering if there was a better way to do it. I've read the routing guide on Rails but little of it makes sense to me right now. I've tried linking directly to the user with
<%= link_to 'Make admin', user, {:controller => 'users', :action => 'make_admin'} %>
But I would receive an error about no route being defined for [POST]/user_id or something along those lines. That feels like the better solution in this case; if it is, what should I do in the routes.rb file to address this?
You can create your routes as follows:
resources :users do
member do
post 'make_admin', as: :make_admin
post 'revoke_admin', as: :revoke_admin
end
end
I hope this helps you out
You can add a helper to your routes like this :
post ':id/make_admin' => 'users#make_admin', as: 'make_admin'
Now make_admin_path(user.id) will link to that. You can view all helpers either by running rake routes in your site's directory or going to localhost:3000/rails/info
As for the error you got, I can't say for certain without seeing the full error, but when you <%= link_to 'text', user %> ruby interprets user at the path, and tries to find a user_path and the {:controller => 'users', :action => 'make_admin'} is a hash of HTML attributes that get added.
I recently started to use rails 4 in college and I'm having troubles with deleting a user I created.
It always sais 'Couldn't find User with 'id'=#:' or 'Couldn't find User without Id'.
I looked up a few solutions as this problem seems to be pretty common but nothing worked.
My user list(Index) looks like this and at the end of every line there is the delete button.
<% #user.each do |u| %>
<tr>
<td>
<%= u.username %> <%= u.email%>
<%= link_to 'Delete', user_path(#user), method: :delete, :confirm => 'Are you sure?'%><br>
</td>
</tr>
<% end %>
This is my destroy method
def destroy
#user = User.find(params[:id])
#user.destroy
if #user.destroy
redirect_to root_url, notice: "User deleted."
end
end
And thats the root page - I suppose I made mistakes here somewhere in 'match' but I'm not sure
resources :users
get 'users/index'
get 'users/new'
get 'users/login'
get 'users/resume'
get 'users/userInformation'
get 'users/update'
root 'users#index'
get 'index'=>'users#index'
get 'new'=>'users#new'
get 'login'=>'users#login'
get 'resume'=>'users#resume'
get 'userInformation'=>'users#userInformation'
get 'update'=>'users#update'
match 'users/:id' => 'users#index', :via => :delete
(I'm using Rails4)
Thanks in advance!
Chrizzly
You need to iterate over your collection and pass each instance of that collection into your link handler:
<%= link_to 'Delete', user_path(u), method: :delete, :confirm => 'Are you sure?'%>
Right now you are passing in #user into your user_path so you're passing in the whole collection, which in this case is an ActiveRecord Relation, not the individual User instance.
Maybe try users_path instead of user_path.
You can find all the routes and their helpers by typing rake:routes in the command line.
For my projects I have the following relevant code
routes:
resources :lists do
resources :items
end
I now included a loop on list/show page in which I want to show the item and provide the users with the possibility to delete the item.
So i got code like this:
<% #items.each do |item|%>
<p>
Item: <%= item.name %>
<%= time_ago_in_words(item.created_at) %> ago.
</p>
<%= link_to "Delete", [#list.item, item], method: :delete %>
<% end %>
But when I try to run it I get the error:
undefined method 'item' for #<List:0x007fba7be6fe28>
While I did define the variables in my controller:
Items-controller:
def destroy
#list = current_user.list
#item = #list.items
end
Could anyone explain whats causing this error?
you have a typo in your controller - it should be #items = #list.items that's why the iteration doesn't work properly.
edit: after formatting your original question, I see that the error was raised on #list object, so you have to fix path to delete action:
<%= link_to "Delete", [#list, item], method: :delete %>
you build the path by providing the parent object (#list) and then the object itself (item) - Rails will translate it to list_item_path.
change your link_to
= link_to("Delete", lists_items_url(#list, #item), method: :delete)
You have defined resources :lists and resources :items.
Basically what you want to do is send a DELETE request to the collection with the IDs to fetch and delete the items.
Run a rake routes to check the exact naming, but I think it's written like that to be sure. Although you don't seem to be needing the IDs based on your controller's action, you still need to supply them for the helper.
You're using #items.each and in your controller have only #item.
I'm trying to figure out how to allow a user to click on a link or button on the index page to clear out all of the objects from the app's database, and then redirect to a newly cleared index page. So, with the example model Article, I expect it should have something to do with an Article.destroy_all method, and I'm expecting it would be a simple solution, but I've tried some variations and am just not sure of how to actually implement it.
So it would be another action in your controller. If we're dealing with Articles then the controller would be:
class ArticlesController < ApplicationController
def indef
#articles = Article.all
end
def destroy_them_all
Article.destroy_all
redirect_to articles_path
end
end
And in the view where you want the user to click on a button to destroy all articles:
<%= link_to 'Destroy them all', destroy_them_all_path, method: :delete, confirm: 'Are you crazy?' %>
Don't forget to add a named route in your routes file:
match '/articles/destroy_them_all', to: 'Articles#destroy_them_all', via: :delete
That should work. Though you might have to check rake routes to make sure I got the destroy_them_all_path correct.
try this:
Article controller:
def destroy_all
#Articles = Article.all
#Articles.each do |a|
a.destroy
end
redirect_to articless_path, notice: "Delted"
end
routes:
post "articles/destroy_all"
view:
<%= form_tag ({ :controller => 'articles', :action => 'destroy_all' }) do%>
<%= submit_tag 'destroy all'%>
<% end %>