Ruby On Rails: ActionController::RoutingError No route matches, action show - ruby-on-rails

I am using this example for uploading files in Ruby on Rails.
I have some problems with routing:
Error:
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"cars", :locale=>#<Car id: 19, car_name: "bwl",, created_at: "2013-01-27 19:12:13", updated_at: "2013-01-27 19:12:13">}):
app/models/arraydb.rb:46:in `to_jq_car'
I have found out that the problem is in the to_jq_car function and in the line:
"delete_url" => car_path(self)
But I have no idea how to correct it.
routes.rb:
resources :cars
match '/show', :to =>'car#index'
root :to => 'cars#index'
cars_controllers.rb
class CarsController < ApplicationController
def index
#cars = Car.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #cars.map{|car| car.to_jq_car } }
end
end
def show
#car = Car.find(params[:id])
respond_to do |format|
format.html
format.json { render json: #car }
end
end
end
and in car.rb I have the following function:
def to_jq_car
{
"name" => (read_attribute(:arraydb_name)).split(".").first,
"url" => car.url(:original),
"delete_url" => car_path(self),
"delete_type" => "DELETE",
}
end
rake routes:
cars GET (/:locale)/cars(.:format) cars#index {:locale=>/en|de|es|ru|zh_cn|ar/}
POST (/:locale)/cars(.:format) cars#create {:locale=>/en|de|es|ru|zh_cn|ar/}
new_car GET (/:locale)/cars/new(.:format) cars#new {:locale=>/en|de|es|ru|zh_cn|ar/}
edit_car GET (/:locale)/cars/:id/edit(.:format) cars#edit {:locale=>/en|de|es|ru|zh_cn|ar/}
car GET (/:locale)/cars/:id(.:format) cars#show {:locale=>/en|de|es|ru|zh_cn|ar/}
PUT (/:locale)/cars/:id(.:format) cars#update {:locale=>/en|de|es|ru|zh_cn|ar/}
DELETE (/:locale)/cars/:id(.:format) cars#destroy {:locale=>/en|de|es|ru|zh_cn|ar/}
show (/:locale)/show(.:format) car#index {:locale=>/en|de|es|ru|zh_cn|ar/}
root /(:locale)(.:format) cars#index {:locale=>/en|de|es|ru|zh_cn|ar/}
Thanks in advance

error causes by :locale param
you have in routes
cars#index {:locale=>/en|de|es|ru|zh_cn|ar/}
so :locale value must be one of this in list en|de|es|ru|zh_cn|ar
and you need to find where you pass this param to controller and correct it
you can use (or maybe you already use) in ApplicationController
def default_url_options
{:locale => I18n.locale}
end

Related

Unable to find working route - No route matches {:action=>"show", :controller=>"users"}

No route matches {:action=>"show", :controller=>"users"}
Working with a condition current_page in rails with this format. Trying to not render a navbar within users/show path, but it should be visible in the rest of site. One thing to note is that the users/show URL has been configured in routes.rb to not show '/users/' folder in the URL, so it looks like 'mysite.com/username'
<% if current_page?(controller: 'users', action: 'show') %>
no navbar
<% else %>
<%= render partial: "shared/navbar" %>
<% end %>
The first condition works fine, however when I reach a page that should match the 'else' condition, for instance my root_path, I get this error:
ActionController::UrlGenerationError in Dashboard#show
Showing /Users/javier/Desktop/rails-apps/testtradus3/app/views/shared/_navbar.html.erb where line #1 raised:
No route matches {:action=>"show", :controller=>"users"}
My route.rb looks like this
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
Rails.application.routes.draw do
...
# This removes '/users/' from user/show URI
resources :users, path: '', :only => [:show]
# User account
devise_for :users,
controllers: {
omniauth_callbacks: "users/omniauth_callbacks",
registrations: "users/registrations",
sessions: "users/sessions"
}
devise_scope :user do
get "session/otp", to: "sessions#otp"
end
resources :users do
resources :builduser, controller: 'users/builduser'
end
...
end
This returns this rails routes:
users GET /users(.:format) users#index
POST /users(.:format) users#create
I have tried removing the custom path in routes.rb, so something like resources :users and that returns these routes
users GET /users(.:format) users#index
POST /users(.:format) users#create
GET /users(.:format) users#index
POST /users(.:format) users#create
GET /users/new(.:format) users#new
GET /users/:id/edit(.:format) users#edit
GET /users/:id(.:format) users#show
My UsersController.rb
class UsersController < ApplicationController
def index
#users = User.all
end
def show
#user = User.friendly.find(params[:id])
#order = Order.new
end
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
# format.html { redirect_to #order, notice: "Order was successfully created." }
# Added this one below:
format.html { redirect_to user_builduser_index_path(#user)}
format.json { render :show, status: :created, location: #user }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
..
end
Going off some of the examples listed in the link below, you could try:
current_page?(users_path(current_user))
# or
current_page?(users_path(#user))
# or even
current_page?("/#{#user&.id}")
An alternative could be to set a before_action in your ApplicationController to set an instance variable:
before_action :display_nav_bar # this would apply to all routes unless overridden like below in the UsersController
def display_nav_bar
#display_nav_bar = true
end
# and in your UsersController:
def show
#display_nav_bar = false
# other stuff
#user = User.find(params[:id)
end
then
<% if #display_nav_bar %>
<%= render partial: "shared/navbar" %>
<% else %>
no navbar
<% end %>
You could also look into using layouts for different controllers and control rendering the nav bar that way.
current_page? source

Destroy Action Acting Unusual

The application has: Clients, which has and belongs to many ActionItems, which has and belongs to many Clients. A user, chooses a client (a client they have as a customer), and adds action items (to do's) to that client. -- Like: User creates => "Email client about X topic," for client: Crayola LLC.
I've been instructed to nest resources like so, in routes:
resources :clients do
resources :action_items
end
So that I can get a URL like:
-http://localhost:3000/clients/42/action_items/11
To display the action items for a specific client.
However - deleting action items for that client, doesn't work. It's been trying to redirect me to the destroy action, on which I get:
undefined local variable or method `clients_action_items' for # <ActionItemsController:0x007febd0edf800>
Prior to this, the delete link, which uses the destroy action, was attempting to redirect me to the show page, on which I was getting:
No route matches [POST] "/clients/42/action_items/1"
Then I added: post '/clients/:client_id/action_items/:id' => 'action_items#destroy' to the routes file. (and now I get the undefined local variable or method clients_action_items' error.
Routes:
Rails.application.routes.draw do
get 'users/index'
get 'users/new'
get 'users/edit'
get 'users/delete'
get 'users/create'
patch 'users/create'
patch 'users/update'
get 'clients/index'
get 'clients/new'
get 'clients/edit'
get 'clients/delete' => 'clients#delete'
get 'clients/create'
patch 'clients/create'
patch 'clients/update'
post '/clients/:client_id/action_items/:id' => 'action_items#destroy'
get 'login', :to => "access#index"
resources :action_items
#/clients/13/action_items
resources :clients do
resources :action_items
end
#get 'home/index'
#get 'home/edit'
#
#get 'home/delete'
#get 'home/show'
root 'home#index'
#define, below **, is the URL we named categories/index. It is now localhost:3000/define
#get 'index' => 'questions#index'
#get 'questions/edit'
#get 'new' => 'questions#new'
#get 'questions/delete'
#post 'questions/destroy'
#get 'questions/show'
#post 'create' => 'questions#create'
match ':controller(/:action(/:id))', :via => [:get, :post]
# end
end
Action Items Controller:
class ActionItemsController < ApplicationController
# before_action :get_owner
def index
#action_items = ActionItem.all
#client = Client.find(params[:client_id])
end
def new
#action_items = ActionItem.new
# #action_items_client = #client.action_items.new
#client = Client.find(params[:client_id])
end
def create
# #action_item = ActionItem.new(action_items_params)
# if #action_item.save
# redirect_to(:action => 'show', :id => #action_item.id)
# #renders client individual page
# else
# redirect_to(:action => 'new')
# end
#client = Client.find(params[:client_id])
#action_item_client = #client.action_items.new(action_items_params)
if #action_item_client.save
redirect_to(:action => 'show', :id => #action_item_client.id, :client_id => #client.id)
else
redirect_to(:action => 'new')
end
end
def edit
#action_item = ActionItem.find(params[:id])
end
def update
#action_item = ActionItem.find(params[:id])
if #action_item.update_attributes(action_items_params)
redirect_to(:controller => 'action_items', :action => 'show', :id => #action_item.id)
flash[:notice] = "Updated"
else
render 'new'
end
end
def show
#client = Client.find(params[:id])
#action_item = ActionItem.find(params[:action_item_id])
end
def action_clients
#action_clients = ActionItem.Client.new
end
def delete
#action_item = #client.action_items.find(params[:client_id])
end
def destroy
# #action_items = #client.action_items.find(params[:id]).destroy
# redirect_to(:controller => 'action_items', :action => 'index')
item = clients_action_items.find(params[:client_id])
item.destroy
if params[:client_id]
redirect_to clients_action_items_path(params[:client_id])
else
redirect_to clients_action_items_path
end
end
private
def action_items_params
params.require(:action_item).permit(:purpose, :correspondence_method, :know_person, :contact_name_answer, :additional_notes)
end
# private
# def get_owner
# if params[:client_id].present?
# #owner = user.clients.find(params[:client_id])
# else
# #owner = user
# end
# end
end
Index view from which I am deleting an action item:
<%= link_to('New Action Item', :controller => 'action_items', :action => 'new') %></br>
<ol><% #action_items.each do |list| %>
<li>
Action Item for <%= #client.name %> is: <strong><%= list.correspondence_method %></strong> Client, about:
<strong><%= list.purpose %> </strong></li>
And you created some additional notes: <strong><%= list.additional_notes %></strong></br></br>
-- Crud Actions -- </br>
<%= link_to('New Action Item', :controller => 'action_items', :action => 'new') %></br>
<%= link_to('Edit Action Item', :controller => 'action_items', :action => 'edit', :id => list.id) %></br>
<%= link_to('Show Individual', :controller => 'action_items', :action => 'show', :id => list.id) %></br>
<%= button_to('Delete Action Item', :controller => 'action_items', :action => 'destroy', :id => list.id) %></br>
<h2> new delete </h2>
</br></br>
<% end %></ol>
I have created the foreign key columns in a migration file with a join table called: action_items_clients:
class CreateActionItemsClients < ActiveRecord::Migration
def change
create_table :action_items_clients, :id => false do |t|
t.integer :action_item_id
t.integer :client_id
end
end
end
-New to rails. Please excuse dirty code. What is wrong here? Why the destroy link issues? Why was the destroy link redirecting to show before, and giving me both routing and ID errors?
Thanks for your time.
*** EDIT ****
Rake routes output:
Prefix Verb URI Pattern Controller#Action
users_index GET /users/index(.:format) users#index
users_new GET /users/new(.:format) users#new
users_edit GET /users/edit(.:format) users#edit
users_delete GET /users/delete(.:format) users#delete
users_create GET /users/create(.:format) users#create
PATCH /users/create(.:format) users#create
users_update PATCH /users/update(.:format) users#update
clients_index GET /clients/index(.:format) clients#index
clients_new GET /clients/new(.:format) clients#new
clients_edit GET /clients/edit(.:format) clients#edit
clients_delete GET /clients/delete(.:format) clients#delete
clients_create GET /clients/create(.:format) clients#create
PATCH /clients/create(.:format) clients#create
clients_update PATCH /clients/update(.:format) clients#update
DELETE /clients/:client_id/action_items/:id(.:format) action_items#destroy
login GET /login(.:format) access#index
action_items GET /action_items(.:format) action_items#index
POST /action_items(.:format) action_items#create
new_action_item GET /action_items/new(.:format) action_items#new
edit_action_item GET /action_items/:id/edit(.:format) action_items#edit
action_item GET /action_items/:id(.:format) action_items#show
PATCH /action_items/:id(.:format) action_items#update
PUT /action_items/:id(.:format) action_items#update
DELETE /action_items/:id(.:format) action_items#destroy
client_action_items GET /clients/:client_id/action_items(.:format) action_items#index
POST /clients/:client_id/action_items(.:format) action_items#create
new_client_action_item GET /clients/:client_id/action_items/new(.:format) action_items#new
edit_client_action_item GET /clients/:client_id/action_items/:id/edit(.:format) action_items#edit
client_action_item GET /clients/:client_id/action_items/:id(.:format) action_items#show
PATCH /clients/:client_id/action_items/:id(.:format) action_items#update
PUT /clients/:client_id/action_items/:id(.:format) action_items#update
DELETE /clients/:client_id/action_items/:id(.:format) action_items#destroy
clients GET /clients(.:format) clients#index
POST /clients(.:format) clients#create
new_client GET /clients/new(.:format) clients#new
edit_client GET /clients/:id/edit(.:format) clients#edit
client GET /clients/:id(.:format) clients#show
PATCH /clients/:id(.:format) clients#update
PUT /clients/:id(.:format) clients#update
DELETE /clients/:id(.:format) clients#destroy
root GET / home#index
GET|POST /:controller(/:action(/:id))(.:format) :controller#:action
It seems you are trying to destroy an object using a POST request.
No route matches [POST] "/clients/42/action_items/1"
Did you try a DELETE request?
delete '/clients/:client_id/action_items/:id' => 'action_items#destroy'
You can read further about CRUD (Create, Read, Update, Delete) operations here
Also, can you run rake:routes and post the output here? That will help figure out what routes are actually being generated.
EDIT
So, as you can see from the output of rake:routes there is a LOT of duplication. You basically have three models, User, Client and ActionItems with basic CRUD and one login.
Rake file:
Rails.application.routes.draw do
get 'login', :to => "access#index"
resources :users
resources :action_items
resources :clients do
member do
get :action_items
end
end
root 'home#index'
end
Client and ActionItems have a many-to-many relation. If it was a one-to-many ie many ActionItems belong to only one Client then it would have been better to nest the resources.
To show all action items of a client you only need one extra method in client controller.
Clients Controller:
def show
#client = Client.find(params[:id])
end
def action_items
#list_action_items = #client.action_items
end
Action Items Controller:
#Will list all action_items irrespective of which clients they belong to
def index
#action_items = ActionItem.all
end
#For creating a new action item,
def new
#action_item = ActionItem.new
#You can render a form with dropdown here with Client.all to assign the new action_item to a client
end
def create
#action_item = ActionItem.new(action_items_params)
if #action_item.save
redirect_to(:action => 'show', :id => #action_item.id)
#renders client individual page
else
redirect_to(:action => 'new')
end
end
def edit
#action_item = ActionItem.find(params[:id])
end
def update
if #action_item.update_attributes(action_items_params)
flash[:notice] = "Updated"
redirect_to(:controller => 'action_items', :action => 'show', :id => #action_item.id)
else
render 'new'
end
end
def show
#action_item = ActionItem.find(params[:id])
end
def destroy
#action_item.destroy
flash[:notice] = "Action Item has been deleted."
redirect_to action_items_path
end
I have tried to simplify the structure here. If you want to perform tasks like deleting all action items of a client from the index/list page of all clients, you can define a method destroy_many in the ActionItems controller which takes client_id as argument, queries all action items and deletes them.
You don't need separate ClientActionItem controller/routes.
Also, if you want to continue with nested routes,
try
<%= button_to('Delete Action Item', client_action_item_path(#client.id, list.id), method: :delete) %></br>
The nested route requires two arguments. The first is client.id and second the action_item id.
This should work, but un-tested. I am not sure of your intentions, but I would use a link_to with a class of button personally.
<%= button_to('Delete Action Item', client_action_item_path(#client, list), method: :delete) %></br>

ActiveRecord::RecordNotFound in Api::V1::PostsController#show

I am trying to follow Rails Todo API Part 1 from egghead.io https://egghead.io/lessons/angularjs-rails-todo-api-part-1
I am trying to access localhost:3000/api/v1/posts and the page should show the json response of:
[]
but instead I get the error:
ActiveRecord::RecordNotFound in Api::V1::PostsController#show
Couldn't find Post without an ID
def show
respond_with(Post.find(params[:id])) <----- error is here
end
I also tried adding a record which should return the json response of that record but I get the same error. What am I doing wrong?
I have completed the controller and routes as follows:
app/controllers/api/v1/posts_controller.rb
module Api
module V1
class PostsController < ApplicationController
skip_before_filter :verify_authenticity_token
respond_to :json
def index
respond_with(Post.all.order("id DESC"))
end
def show
respond_with(Post.find(params[:id]))
end
def create
#post = Post.new(post_params)
if #post.save
respond_to do |format|
format.json {render :json => #post}
end
end
end
def update
#post = Post.find(params[:id])
if #post.update(post_params)
respond_to do |format|
format.json {render :json => #post}
end
end
end
def destroy
respond_with Post.destroy(params[:id])
end
private
def post_params
params.require(:post).permit(:content)
end
end
end
end
config/routes.rb
WhatRattlesMyCage::Application.routes.draw do
namespace :api, defaults: {format: :json} do
namespace :v1 do
resource :posts
end
end
end
rake routes:
Prefix Verb URI Pattern Controller#Action
api_v1_posts POST /api/v1/posts(.:format) api/v1/posts#create {:format=>:json}
new_api_v1_posts GET /api/v1/posts/new(.:format) api/v1/posts#new {:format=>:json}
edit_api_v1_posts GET /api/v1/posts/edit(.:format) api/v1/posts#edit {:format=>:json}
GET /api/v1/posts(.:format) api/v1/posts#show {:format=>:json}
PATCH /api/v1/posts(.:format) api/v1/posts#update {:format=>:json}
PUT /api/v1/posts(.:format) api/v1/posts#update {:format=>:json}
DELETE /api/v1/posts(.:format) api/v1/posts#destroy {:format=>:json}
Make it resources :posts in your routes.rb
As it is you have malformed routes and the show method is being called instead of the index method

Add Edit and Delete feature in comment polymorphic-association-revised posts

I read this http://railscasts.com/episodes/154-polymorphic-association-revised posts and implement it as it have. But i want to add edit and delete features also in this tuotorial.
I have comments_controller.rb is like this
class CommentsController < ApplicationController
before_filter :load_commentable
def index
#comments = #commentable.comments
end
def new
#comment = #commentable.comments.new
end
def create
#comment = #commentable.comments.new(params[:comment])
if #comment.save
redirect_to #commentable, notice: "Comment created."
else
render :new
end
end
private
def load_commentable
resource, id = request.path.split('/')[1, 2]
#commentable = resource.singularize.classify.constantize.find(id)
end
# def load_commentable
# klass = [Article, Photo, Event].detect { |c| params["#{c.name.underscore}_id"] }
# #commentable = klass.find(params["#{klass.name.underscore}_id"])
# end
end
my _comments.html.erb as given is like this
<div id="comments">
<% #comments.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
</div>
<% end %>
</div>
my routes is like this
Blog::Application.routes.draw do
resources :articles do
resources :comments
end
resources :photos do
resources :comments
end
resources :events do
resources :comments
end
resources :comments
root to: 'articles#index'
end
My rake routes is like this
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
photo_comments GET /photos/:photo_id/comments(.:format) comments#index
POST /photos/:photo_id/comments(.:format) comments#create
new_photo_comment GET /photos/:photo_id/comments/new(.:format) comments#new
edit_photo_comment GET /photos/:photo_id/comments/:id/edit(.:format) comments#edit
photo_comment GET /photos/:photo_id/comments/:id(.:format) comments#show
PUT /photos/:photo_id/comments/:id(.:format) comments#update
DELETE /photos/:photo_id/comments/:id(.:format) comments#destroy
photos GET /photos(.:format) photos#index
POST /photos(.:format) photos#create
new_photo GET /photos/new(.:format) photos#new
edit_photo GET /photos/:id/edit(.:format) photos#edit
photo GET /photos/:id(.:format) photos#show
PUT /photos/:id(.:format) photos#update
DELETE /photos/:id(.:format) photos#destroy
event_comments GET /events/:event_id/comments(.:format) comments#index
POST /events/:event_id/comments(.:format) comments#create
new_event_comment GET /events/:event_id/comments/new(.:format) comments#new
edit_event_comment GET /events/:event_id/comments/:id/edit(.:format) comments#edit
event_comment GET /events/:event_id/comments/:id(.:format) comments#show
PUT /events/:event_id/comments/:id(.:format) comments#update
DELETE /events/:event_id/comments/:id(.:format) comments#destroy
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
root / articles#index
Like so....? This assumes your routes are nested for :Edit, and :update actions under the "commentable" routes.
def edit
#comment = #commentable.comments.find(params[:id])
end
def create
#comment = #commentable.comments.find(params[:id])
if #comment.update_attributes(params[:comment])
redirect_to #commentable, notice: "Comment updated."
else
render :edit
end
end
Your edit should look like this:
<%= link_to 'Edit', [:edit, #comment.commentable, #comment] %>
However... you will want to have some kind of authentication and/or authorization.
I don't have a lot of time to look this over right now, but in response to your tweet I'm posting the code I was using in my test app that I got to work. I'm using HAML here.
When calling for an edit, or delete link in my view I used this partial:
- #category.photos.each do |photo|
= image_tag photo.image_url(:thumb)
%figcaption
= link_to "Change", [:edit, #category, photo]
= link_to "Delete", [#category, photo], :method => :delete
= link_to "New photo", [:new, #category, :photo]
My photos controller:
class PhotosController < ApplicationController
def create
#category = Category.find(params[:category_id])
#photo = #category.photos.create!(params[:photo])
redirect_to #category, :notice => "Photo created."
end
def edit
#category = Category.find(params[:category_id])
#photos = #category.photos
#photo = #photos.find(params[:id])
end
def update
#category = Category.find(params[:category_id])
#photo = #category.photos.find(params[:id])
respond_to do |format|
if #photo.update_attributes(params[:photo])
format.html { redirect_to #category, notice: "<i class=icon-ok /> #{#category.name} was successfully updated."}
format.js
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #category.errors, status: :unprocessable_entity }
end
end
end
def new
#category = Category.find(params[:category_id])
#photo = #category.photos.new
end
def destroy
#category = Category.find(params[:category_id])
#photos = #category.photos
#photo = #photos.find(params[:id])
#photo.destroy
redirect_to #category, :notice => "Photo deleted."
end
end
In my photo model I have:
class Photo
include Mongoid::Document
embedded_in :category, :inverse_of => :photos
field :image
attr_accessible :image
# Set uploader
mount_uploader :image, ImageUploader
end
In the category model:
class Category
# Includes to set up the model
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Ancestry
include Mongoid::Versioning
include Mongoid::Paranoia
include Mongoid::Slug
include Mongoid::History::Trackable
# tell it that it can go nest itself
has_ancestry
embeds_many :photos
# Accept nested attributes
accepts_nested_attributes_for :photos, :autosave => true
# tell history how it can track things
track_history :on => [:name, :description], # track these fields
#:modifier_field => :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
:version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
:track_create => false, # track document creation, default is false
:track_update => true, # track document updates, default is true
:track_destroy => false # track document destruction, default is false
# Keep at most 5 versions of a record
max_versions 5
# Add the models fields here
field :name, type: String
field :ancestry, type: String
field :description, type: String
# Set which field the url slug should use
slug :name
# Make sure these attributes can be accessed
attr_accessible :ancestry, :name, :parent_id, :description
# Make name fields capitalized on save
def name=(t)
write_attribute(:name, t.to_s.split(' ').map {|w| w.capitalize}.join(' '))
end
# Create some scopes
scope :except, lambda{ |category| where("id <> ?", category.id)}
end
Notes:
I just pasted a bunch of my code, cause I didn't have too much time to parse it down to answer specifically.
Hopefully the way I've written some of my code can help you.
Keep in mind that I'm using:
Mongoid
Rails 3
Cheers,
-Brandon
This is the destroy action for polymorphic association, I am sure you can figure out the Edit :)
VIEW
<%= link_to content_tag(:i, "", class: "icon-trash icons"), [#commentable, comment], method: :delete,
data: { confirm: "Are you sure?" },
title: "Delete" %>
CONTROLLER
def destroy
#comment = Comment.find(params[:id])
#commentable = #comment.commentable
if #comment.destroy
flash[:success] = "Comment Destroyed!"
redirect_to :back
end
end

Ruby on Rails: Routing error

I am having trouble deleting and showing user records.
Here is my routes.rb
FinalApp::Application.routes.draw do
resources :users
devise_for :users, :controllers => { :registrations => 'admin' }
resources :projects
match "search" => "projects#search", :as => :search
root :to => 'projects#index'
end
Here is my admin controller:
class AdminController < ApplicationController
def index
#users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #users }
end
end
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render json: #user, status: :created, location: #user }
else
format.html { render action: "new" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# GET /users/1
# GET /users/1.json
def show
#user = User.find(params[:id])
#user_user_id = params[:id]
respond_to do |format|
format.html # show.html.erb
format.json { render json: #user }
end
end
# GET /users/new
# GET /users/new.json
def new
#user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #user }
end
end
# GET /users/1/edit
def edit
#user = User.find(params[:id])
end
# POST /users
# POST /users.json
# PUT /users/1
# PUT /users/1.json
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#user = User.find(params[:id])
#user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
end
Here is my view:
<%= stylesheet_link_tag "admin" %>
<body>
<div id ="title1">Admin</div>
<div class ="menu"></div>
<div id ="section3">
<table id = "mytable">
<table border = "1">
<tr>
<th>Username </th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Admin?</th>
<th></th>
<th></th>
<th></th>
</tr>
<%= link_to "New User", admin_new_path %><br />
<% #users.each do |t| %>
<tr>
<td><%= t.username %></td>
<td><%= t.email %></td>
<td><%= t.firstname %></td>
<td><%= t.lastname %></td>
<td><%= t.admin %></td>
<td><%= link_to 'Show', t %></td>
<td> <%= button_to "Delete", t, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table></br>
</body>
</html>
I can display the User database, but when I go to delete a record. I get this error No route matches [DELETE] "/users/11". I am new to rails so please remember this when trying to help. Thanks in advance.
Edit: here are my routes =>
admin_index GET /admin(.:format) admin#index
POST /admin(.:format) admin#create
new_admin GET /admin/new(.:format) admin#new
edit_admin GET /admin/:id/edit(.:format) admin#edit
admin GET /admin/:id(.:format) admin#show
PUT /admin/:id(.:format) admin#update
DELETE /admin/:id(.:format) admin#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) admin#cancel
user_registration POST /users(.:format) admin#create
new_user_registration GET /users/sign_up(.:format) admin#new
edit_user_registration GET /users/edit(.:format) admin#edit
PUT /users(.:format) admin#update
DELETE /users(.:format) admin#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
search /search(.:format) projects#search
root / projects#index
EDIT2: This is how my routes.rb file should have looked. Using rake routes I was able to change the paths to fix my problem.
FinalApp::Application.routes.draw do
# website home
root :to => 'projects#index'
# devise sessions (NB does not use admin/users controller)
devise_for :users, :controllers => { :registrations => 'users' }
# normal controllers
resources :users
resources :projects
# custom routes
match "search" => "projects#search", :as => :search
end
You should add resources :users to your routes.rb. In any case, you can always check rake routes in your console to see the available routes.
On a side note, the way you define you admin routes are not completely correct. Not everything is a get. For example, creating an admin would be a post. The easiest way is just to use something like resources :admins.
You have ended up with two routes leading to admin#destroy:
DELETE /users(.:format) admin#destroy
DELETE /admin/:id(.:format) admin#destroy
and yet you have no route which matches /users/xx, just a route which matches DELETE /users with an optional format bit. You have a route which matches /admin/11 so if you tried that it would work, however I would try to simplify things a bit.
Do you actually need to specify a controller on the devise resources? What exactly do you want to override there, as you have ended up with a load of routes (like cancel) which lead nowhere and some which clash...
Try a simpler routes definition (NB this requires renaming your AdminController UsersController instead, I would follow this convention as it will make your life easier, and match your other urls, so you end up with users/1 etc, not admin/1)
FinalApp::Application.routes.draw do
# website home
root :to => 'projects#index'
# devise sessions (NB does not use admin/users controller)
devise_for :users
# normal controllers
resources :users
resources :projects
# custom routes
match "search" => "projects#search", :as => :search
end
Then do rake routes to make sure you understand where the routes are pointing. You need a route which says (NB the :id bit):
DELETE /users/:id(.:format) users#destroy
or if you prefer an admin controller (and are willing to sort out the custom routes)
DELETE /admin/:id(.:format) admin#destroy
Perhaps before diving into this though you could read through the rails routing guide as it might clear a few things up for you:
http://guides.rubyonrails.org/routing.html
This appears to be a pretty standard use of Devise with your only notable difference being the name of your Controller. Therefore, the only thing you should need for devise routing is:
devise_for :users, :controllers => { :registrations => 'admin' }
Also, delete all of your "get 'admin/*'" entries. Not every HTTP method is a GET when you're working in a REST environment. Here is one article that discusses REST methods.

Resources