I have encountered a routing issue with rails. I'll explain as best as I can.
a user can use the create and index actions of the entries controller.
authenticated with devise, an admin can perform the rest of the restful actions on the entries controller. Furthermore, there are an additional three pages which I wish to add, 'pending, approved, rejected', so I added these to my entries controller like so:
class EntriesController < ApplicationController
before_action :get_entries, only: [:index, :pending, :approved, :rejected]
expose(:entry){#entry}
expose(:entries){#entries}
def create
#entry = Photo.new(params_entry)
if #entry.save
record_saved
return redirect_to(entries_path)
else
check_for_errors
return render('new')
end
end
def index
end
def new
#entry = Photo.new
end
def show
end
def pending
end
def approved
end
def rejected
end
private
def params_photo
params.require(:photo).permit!
end
def get_photos
#entries = Entry.all
end
end
(note, this controller is incomplete, but for the purposes of my question it suffices)
I am trying to configure my routes so that the path to view these pages are like
http://localhost:3000/admin/entries/pending
so I have configured my routes like so:
devise_for :admins # The priority is based upon order of creation: first created -> highest priority.
root 'home#index'
resources :entries, :only => [:index, :new]
namespace :admin do
resources :entries do
collection do
match :pending, :via => [:get, :post]
match :rejected, :via => [:get, :post]
match :approved, :via => [:get, :post]
end
end
end
except I am getting the error
uninitialized constant Admin::EntriesController
expect if i change 'admin' to plural 'admins' and configure my view path accordingly I get this error:
No route matches [GET] "/admin/entries/pending"
why is this happening? Am i making the correct use of 'namespace' and 'collection'. How do I fix this issue?
rake routes:
Prefix Verb URI Pattern Controller#Action
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy
admin_password POST /admins/password(.:format) devise/passwords#create
new_admin_password GET /admins/password/new(.:format) devise/passwords#new
edit_admin_password GET /admins/password/edit(.:format) devise/passwords#edit
PATCH /admins/password(.:format) devise/passwords#update
PUT /admins/password(.:format) devise/passwords#update
cancel_admin_registration GET /admins/cancel(.:format) devise/registrations#cancel
admin_registration POST /admins(.:format) devise/registrations#create
new_admin_registration GET /admins/sign_up(.:format) devise/registrations#new
edit_admin_registration GET /admins/edit(.:format) devise/registrations#edit
PATCH /admins(.:format) devise/registrations#update
PUT /admins(.:format) devise/registrations#update
DELETE /admins(.:format) devise/registrations#destroy
root GET / home#index
entries GET /entries(.:format) entries#index
new_entry GET /entries/new(.:format) entries#new
pending_admin_entries GET|POST /admin/entries/pending(.:format) admin/entries#pending
rejected_admin_entries GET|POST /admin/entries/rejected(.:format) admin/entries#rejected
approved_admin_entries GET|POST /admin/entries/approved(.:format) admin/entries#approved
admin_entries GET /admin/entries(.:format) admin/entries#index
POST /admin/entries(.:format) admin/entries#create
new_admin_entry GET /admin/entries/new(.:format) admin/entries#new
edit_admin_entry GET /admin/entries/:id/edit(.:format) admin/entries#edit
admin_entry GET /admin/entries/:id(.:format) admin/entries#show
PATCH /admin/entries/:id(.:format) admin/entries#update
PUT /admin/entries/:id(.:format) admin/entries#update
DELETE /admin/entries/:id(.:format) admin/entries#destroy
FOLDER STRUCTURE
controllers > admin_controller.rb
admin > entries_controller.rb (this is the one with pending etc)
entries_controller.rb (this is the public one with index etc)
views > admins > index.html.erb
entries > pending.html.erb, rejected.html.erb etc
Thanks
Related
I want to get in my browser url field like this:
/posts/title_post instead /posts/id
I have written:
routes.rb:
# added param: :title
resources :posts, param: :title do
resources :comments, only: [:new, :create]
resources :images do
resources :comments, only: [:new, :create]
end
resources :links do
resources :comments, only: [:new, :create]
end
resources :photos, only: [:new, :create,:destroy]
resources :songs, only: [:new, :create, :destroy]
end
bundle exec rake routes:
/posts/new(.:format) posts#new
/posts/:title/edit(.:format) posts#edit
/posts/:title(.:format) posts#show
/posts/:title(.:format) posts#update
/posts/:title(.:format) posts#update
/posts/:title(.:format) posts#destroy
post_controller.rb:
def set_post
#post = Post.find(params[:title])
end
Blog works, but url is post/5 (for example). How to make url like: post/my_post_title?
class PostsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :show]
respond_to :html, :json, :rss, :atom
def index
if params[:search].blank?
#posts = Post.includes(:comments, :photos).all
else
#search = Post.search do
fulltext params[:search]
end
#posts = #search.results
end
respond_with(#posts)
end
def show
set_post
end
def new
#post = Post.new
end
def edit
set_post
end
def create
#post = Post.new(post_params)
# responder gem doc
#post.errors.add(:base, :invalid) unless #post.save#test
respond_with(#post)
end
def update
set_post
if #post.valid?
#post.update(post_params)
else
# responder gem doc
#post.errors.add(:base, :invalid)
end
respond_with(#post)
end
def destroy
set_post
#post.destroy
respond_with(#post)
end
def feed
#posts = Post.all.reverse
respond_with(#posts)
end
def archive
#posts_by_year = Post.limit(300).all.order("created_at DESC").
group_by {|post| post.created_at.beginning_of_year}
end
private
def set_post
#post = Post.find(params[:title])
end
def post_params
params.require(:post).permit(:title, :content)
end
end
All routes:
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# feed GET /feed(.:format) posts#feed
# archive GET /archive(.:format) posts#archive
# 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_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) callbacks#passthru
# user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) callbacks#facebook
# 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
# PATCH /users/password(.:format) devise/passwords#update
# PUT /users/password(.:format) devise/passwords#update
# cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
# user_registration POST /users(.:format) devise/registrations#create
# new_user_registration GET /users/sign_up(.:format) devise/registrations#new
# edit_user_registration GET /users/edit(.:format) devise/registrations#edit
# PATCH /users(.:format) devise/registrations#update
# PUT /users(.:format) devise/registrations#update
# DELETE /users(.:format) devise/registrations#destroy
# root GET / posts#index
# post_comments POST /posts/:post_title/comments(.:format) comments#create
# new_post_comment GET /posts/:post_title/comments/new(.:format) comments#new
# post_image_comments POST /posts/:post_title/images/:image_id/comments(.:format) comments#create
# new_post_image_comment GET /posts/:post_title/images/:image_id/comments/new(.:format) comments#new
# post_images GET /posts/:post_title/images(.:format) images#index
# POST /posts/:post_title/images(.:format) images#create
# new_post_image GET /posts/:post_title/images/new(.:format) images#new
# edit_post_image GET /posts/:post_title/images/:id/edit(.:format) images#edit
# post_image GET /posts/:post_title/images/:id(.:format) images#show
# PATCH /posts/:post_title/images/:id(.:format) images#update
# PUT /posts/:post_title/images/:id(.:format) images#update
# DELETE /posts/:post_title/images/:id(.:format) images#destroy
# post_link_comments POST /posts/:post_title/links/:link_id/comments(.:format) comments#create
# new_post_link_comment GET /posts/:post_title/links/:link_id/comments/new(.:format) comments#new
# post_links GET /posts/:post_title/links(.:format) links#index
# POST /posts/:post_title/links(.:format) links#create
# new_post_link GET /posts/:post_title/links/new(.:format) links#new
# edit_post_link GET /posts/:post_title/links/:id/edit(.:format) links#edit
# post_link GET /posts/:post_title/links/:id(.:format) links#show
# PATCH /posts/:post_title/links/:id(.:format) links#update
# PUT /posts/:post_title/links/:id(.:format) links#update
# DELETE /posts/:post_title/links/:id(.:format) links#destroy
# post_photos POST /posts/:post_title/photos(.:format) photos#create
# new_post_photo GET /posts/:post_title/photos/new(.:format) photos#new
# post_photo DELETE /posts/:post_title/photos/:id(.:format) photos#destroy
# post_songs POST /posts/:post_title/songs(.:format) songs#create
# new_post_song GET /posts/:post_title/songs/new(.:format) songs#new
# post_song DELETE /posts/:post_title/songs/:id(.:format) songs#destroy
# posts GET /posts(.:format) posts#index
# POST /posts(.:format) posts#create
# new_post GET /posts/new(.:format) posts#new
# edit_post GET /posts/:title/edit(.:format) posts#edit
# post GET /posts/:title(.:format) posts#show
# PATCH /posts/:title(.:format) posts#update
# PUT /posts/:title(.:format) posts#update
# DELETE /posts/:title(.:format) posts#destroy
#
Try this ........
post_controller.rb:
Find will only work with params[:id]
def set_post
#post = Post.find(params[:id])
end
To find with title you need to use :
def set_post
#post = Post.find_by(title: params[:title])
end
Here, to make url like: post/my_post_title? there is a good Gem, You can also try this...
You need to use FriendlyId gem
# Gemfile
gem 'friendly_id'
rails generate friendly_id
rake db:migrate
# edit app/models/post.rb
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title_post, use: :slugged
end
# If you're adding FriendlyId to an existing app and need
# to generate slugs for existing users, do this from the
# console, runner, or add a Rake task:
Post.find_each(&:save)
For more details please refer this link freindly_id gem
Hope this will work for you.
I'm creating a simple Todo list application in rails and I have the next problem:
I have a task that belongs_to project
I've created a template - projects/show.html.erb
And I can't set it to be the root page of my app, as I can see from the debugger - root is always projects#index action
P.S. I need show to be the root page cause I can't use this form in index action
<%= form_for [#project, #task],remote: true do |f| %>
<%= f.input :body,class: 'form-control' %>
<%= f.submit 'Add task', class: 'btn' %>
<% end %>
projects controller
class ProjectsController < ApplicationController
before_action :load_project, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
#projects = current_user.projects unless current_user.nil?
end
def show
#task = #project.tasks.new
end
def new
#project = current_user.projects.new
end
def edit
end
def create
#project = current_user.projects.create(project_params)
if #project.save
redirect_to root_path
else
render :new
end
end
def update
if #project.update(project_params)
redirect_to #project
else
render :edit
end
end
def destroy
#project.destroy
redirect_to projects_path
end
private
def load_project
begin
#project = Project.find(params[:id]) #raises an exception if project not found
rescue ActiveRecord::RecordNotFound
redirect_to projects_path
end
end
def project_params
params.require(:project).permit(:name, :user_id)
end
end
Routes.rb
Rails.application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'projects#index'
resources :projects do
resources :tasks
end
end
And routes generated by rake routes
todo$ rake routes
Prefix Verb URI Pattern Controller#Action
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_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) callbacks#passthru
user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) callbacks#facebook
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
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / home#index
project_tasks GET /projects/:project_id/tasks(.:format) tasks#index
POST /projects/:project_id/tasks(.:format) tasks#create
new_project_task GET /projects/:project_id/tasks/new(.:format) tasks#new
edit_project_task GET /projects/:project_id/tasks/:id/edit(.:format) tasks#edit
project_task GET /projects/:project_id/tasks/:id(.:format) tasks#show
PATCH /projects/:project_id/tasks/:id(.:format) tasks#update
PUT /projects/:project_id/tasks/:id(.:format) tasks#update
DELETE /projects/:project_id/tasks/:id(.:format) tasks#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
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
Why do you want the index to be show page? Well, how can you do that? The show action requires a id to be passed to the controller so that a particular project can be displayed to the user. How can you pass in a id when the user is requesting for /?
Does that make sense?
I would suggest you to leave the root to the index page. In the index view, link each project to its show page. Its unnecessary to have a nested form in the index page.
Or if you want to list all projects and tasks in the root page, modify your index view to loop through each project's tasks.
#projects.each do |project|
# display project's information
project.tasks.each do |task|
# display the task information
# display a new task button
end
end
But you can't display the nested form like you asked. Because the form requires #project and #task which you can't determine in the index action. Maybe you can add remote: true to the "New task" and then trigger a JS response to render the form in a modal.
If this sounds new to you, please see https://launchschool.com/blog/the-detailed-guide-on-how-ajax-works-with-ruby-on-rails
This will walk through you using AJAX in your rails application.
Hope this helps!
hi,
I am having a problem with my routes. Just noticed a strange thing. I have no :id in my show/edit/... routes.
This is my rake.routes output:
WARNING: Nokogiri was built against LibXML version 2.9.0, but has dynamically loaded 2.9.2
Prefix Verb URI Pattern Controller#Action
new_person_session GET /persons/sign_in(.:format) devise/sessions#new
person_session POST /persons/sign_in(.:format) devise/sessions#create
destroy_person_session DELETE /persons/sign_out(.:format) devise/sessions#destroy
person_password POST /persons/password(.:format) devise/passwords#create
new_person_password GET /persons/password/new(.:format) devise/passwords#new
edit_person_password GET /persons/password/edit(.:format) devise/passwords#edit
PATCH /persons/password(.:format) devise/passwords#update
PUT /persons/password(.:format) devise/passwords#update
supplier_password POST /suppliers/password(.:format) devise/passwords#create
new_supplier_password GET /suppliers/password/new(.:format) devise/passwords#new
edit_supplier_password GET /suppliers/password/edit(.:format) devise/passwords#edit
PATCH /suppliers/password(.:format) devise/passwords#update
PUT /suppliers/password(.:format) devise/passwords#update
facilitator_password POST /facilitators/password(.:format) devise/passwords#create
new_facilitator_password GET /facilitators/password/new(.:format) devise/passwords#new
edit_facilitator_password GET /facilitators/password/edit(.:format) devise/passwords#edit
PATCH /facilitators/password(.:format) devise/passwords#update
PUT /facilitators/password(.:format) devise/passwords#update
customer_password POST /customers/password(.:format) devise/passwords#create
new_customer_password GET /customers/password/new(.:format) devise/passwords#new
edit_customer_password GET /customers/password/edit(.:format) devise/passwords#edit
PATCH /customers/password(.:format) devise/passwords#update
PUT /customers/password(.:format) devise/passwords#update
cancel_customer_registration GET /customers/cancel(.:format) devise/registrations#cancel
customer_registration POST /customers(.:format) devise/registrations#create
new_customer_registration GET /customers/sign_up(.:format) devise/registrations#new
edit_customer_registration GET /customers/edit(.:format) devise/registrations#edit
PATCH /customers(.:format) devise/registrations#update
PUT /customers(.:format) devise/registrations#update
DELETE /customers(.:format) devise/registrations#destroy
home_index GET /home/index(.:format) home#index
root GET / home#index
suppliers_index GET /suppliers/index(.:format) suppliers#index
facilitators_index GET /facilitators/index(.:format) facilitators#index
customers_index GET /customers/index(.:format) customers#index
new_customers GET /customers/new(.:format) customers#new
customers_newCustomer POST /customers/newCustomer(.:format) customers#newCustomer
customers_editCustomer GET /customers/editCustomer(.:format) customers#editCustomer
parties POST /parties(.:format) parties#create
new_parties GET /parties/new(.:format) parties#new
edit_parties GET /parties/edit(.:format) parties#edit
GET /parties(.:format) parties#show
PATCH /parties(.:format) parties#update
PUT /parties(.:format) parties#update
DELETE /parties(.:format) parties#destroy
parties_index GET /parties/index(.:format) parties#index
visitors_new POST /visitors/new(.:format) visitors#new
visitors_create POST /visitors/create(.:format) visitors#create
this is my routes.rb:
Rails.application.routes.draw do
# devise_for :people
devise_for :persons, :skip => :registrations
devise_for :suppliers, :facilitators, skip: [:registrations, :sessions]
devise_for :customers, :skip => :sessions
# routes for all users
authenticated :persons do
end
# routes only for customers
authenticated :customers, lambda {|u| u.type == "Customer"} do
end
# routes only for companies
authenticated :facilitators, lambda {|u| u.type == "Facilitator"} do
end
# routes only for suppliers
authenticated :suppliers, lambda {|u| u.type == "Supplier"} do
end
# mde toevoegeingen
get 'home/index'
root to: "home#index"
get 'suppliers/index'
get 'facilitators/index'
get 'customers/index'
resource :customers, :only => :new
match 'customers/newCustomer', to: 'customers#newCustomer', via: :post
match 'customers/editCustomer', to: 'customers#editCustomer', via: :get
resource :parties
get 'parties/index'
match 'visitors/new', to: 'visitors#new', via: :post
match 'visitors/create', to: 'visitors#create', via: :post
end
My urls show a '.' instead of a '/' before the 'id'. this is an example url for the show method of a model:
http://localhost:3000/parties.2
The strange thing is that the normal routing is working. Strange because the dot is intended for defining the format.
Now I need the format becase of a remote: true behaviour I am implementing I am getting into troubles.
What did I do wrong?
regards Martijn
Try to change:
resource :parties
To:
resources :parties
Just like Brad Werth said in comments.
the plural resources
resources :photos
creates
GET /photos Photos index display a list of all photos
GET /photos/new Photos new return an HTML form for creating a new photo
POST /photos Photos create create a new photo
GET /photos/1 Photos show display a specific photo
GET /photos/1/edit Photos edit return an HTML form for editing a photo
PUT /photos/1 Photos update update a specific photo
DELETE /photos/1 Photos destroy delete a specific photo
in contrast to Singular Resources
You can also apply RESTful routing to singleton resources within your
application. In this case, you use map.resource instead of
map.resources and the route generation is slightly different. For
example, a routing entry of
resource :geocoded
GET /geocoder/new Geocoders new return an HTML form for creating the new geocoder
POST /geocoder Geocoders create create the new geocoder
GET /geocoder Geocoders show display the one and only geocoder resource
GET /geocoder/edit Geocoders edit return an HTML form for editing the geocoder
PUT /geocoder Geocoders update update the one and only geocoder resource
DELETE /geocoder Geocoders destroy delete the geocode resource
I'm new to RoR and Devise and i'm stuck in User Authentications with Devise. I'm developing a web site, which has admin pages in it. My structure looks like this:
app
|-controllers
|-admin
|- user_list_controller.rb //Every user crud operations are in it.
|-views
|-admin
|-user_list
|-new.html.erb
|-edit.html.erb
|-devise //Also have devise views
UserListController:
class Admin::UserListController < ApplicationController
layout 'admin/admin'
def index
#user_list = User.all
end
def new
#user = User.new
end
def edit
end
def delete
end
def create_user
end
end
What I want to do is that, I want to use Devise methods under this controller but this is where I stuck in. I created a UserController which base is Devise::RegistrationsController. But this time I got this error,
Could not find devise mapping for path "/admin/create_user". This may
happen for two reasons: 1) You forgot to wrap your route inside the
scope block. For example: devise_scope :user do get "/some/route" =>
"some_devise_controller" end 2) You are testing a Devise controller
bypassing the router. If so, you can explicitly tell Devise which
mapping to use: #request.env["devise.mapping"] =
Devise.mappings[:user]
It looks like a route.rb error and my route file is:
Rails.application.routes.draw do
devise_for :users
devise_scope :user do
# post "admin/add_user" =>"admin/user_list#create_user", as: :adduser
end
namespace :admin do
root to: 'admin#index'
get 'user_list', :to => 'user_list#index'
post 'create_user', :to => "user#create"
get 'new_user', :to => 'user_list#new'
get 'user_proposals', :to => 'user_proposal_forms#index'
get 'user_appointments', :to => 'user_appointments#index'
get 'brands', :to => 'brands#index'
get 'brand_makes', :to => 'brand_makes#index'
get 'make_types', :to => 'make_types#index'
end
end
And the result of rake routes is this:
Prefix Verb URI Pattern Controller#Action 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
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format)devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
admin_root GET /admin(.:format) admin/admin#index
admin_user_list GET /admin/user_list(.:format) admin/user_list#index
admin_create_user POST /admin/create_user(.:format) admin/user#create
admin_new_user GET /admin/new_user(.:format) admin/user_list#new
admin_user_proposalsGET /admin/user_proposals(.:format) admin/user_proposal_forms#index
admin_user_appointments GET /admin/user_appointments(.:format) admin/user_appointments#index
admin_brands GET /admin/brands(.:format) admin/brands#index
admin_brand_makes GET /admin/brand_makes(.:format) admin/brand_makes#index
admin_make_types GET /admin/make_types(.:format) admin/make_types#index
It looks messy, sorry for that. Finally my form_for looks like this:
<%= simple_form_for #user, url: admin_create_user_path, class: "form-horizontal" do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<% end %>
So where did I make a mistake? I've read all the documents in Devise, tried so many things but couldn't solve the problem.
In routes file use
devise_for :users, :controllers => { registrations: 'registrations' }
i would like to perform an action on some of my routes, the problem is they are not in a 'resources' block because I need named methods for each. I want to be able to toggle the state of each an attribute in each item in an index type view. I wwas attempting to incorporate this tutorial.
devise_for :admins # The priority is based upon order of creation: first created -> highest priority.
root 'home#index'
resources :entries, :only => [:index, :new, :create]
namespace :admin do
namespace :entries do
match :pending, :via => [:get, :post], :collection => { :toggle_approve => :put}
match :rejected, :via => [:get, :post], :collection => { :toggle_approve => :put}
match :approved, :via => [:get, :post], :collection => { :toggle_approve => :put}
end
end
entries controller
class Admin::EntriesController < ApplicationController
expose(:entries){#entries}
def index
end
def show
end
def approved
#entries = Photo.with_approved_state
end
def pending
#entries = Photo.with_pending_state
end
def rejected
#entries = Photo.with_rejected_state
end
def toggle_approve
#a = Photo.find(params[:id])
#a.toggle!(:workflow_state)
render :nothing => true
end
rake routes
Prefix Verb URI Pattern Controller#Action
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy
admin_password POST /admins/password(.:format) devise/passwords#create
new_admin_password GET /admins/password/new(.:format) devise/passwords#new
edit_admin_password GET /admins/password/edit(.:format) devise/passwords#edit
PATCH /admins/password(.:format) devise/passwords#update
PUT /admins/password(.:format) devise/passwords#update
cancel_admin_registration GET /admins/cancel(.:format) devise/registrations#cancel
admin_registration POST /admins(.:format) devise/registrations#create
new_admin_registration GET /admins/sign_up(.:format) devise/registrations#new
edit_admin_registration GET /admins/edit(.:format) devise/registrations#edit
PATCH /admins(.:format) devise/registrations#update
PUT /admins(.:format) devise/registrations#update
DELETE /admins(.:format) devise/registrations#destroy
root GET / home#index
entries GET /entries(.:format) entries#index
POST /entries(.:format) entries#create
new_entry GET /entries/new(.:format) entries#new
admin_entries_pending GET|POST /admin/entries/pending(.:format) admin/entries#pending {:collection=>{:toggle_approve_article=>:put}}
admin_entries_rejected GET|POST /admin/entries/rejected(.:format) admin/entries#rejected {:collection=>{:toggle_approve_article=>:put}}
admin_entries_approved GET|POST /admin/entries/approved(.:format) admin/entries#approved {:collection=>{:toggle_approve_article=>:put}}
I don't know where the collection option is from (I literally can't find reference to it anywhere)
Implementing non-resourceful routes is actually relatively simple:
#config/routes.rb
resources :entries, only: [:index, :new, :create] do
collection do
match :pending, via: [:get, :post] #-> domain.com/entries/pending
match :rejected, via: [:get, :post] #-> domain.com/entries/rejected
match :approved, via: [:get, :post] #-> domain.com/entries/approved
end
end
I don't understand the collection option - I don't think that belongs in your routes. Although having thought about it, I guess you're trying to make it so that if you receive a request to domain.com/entries/toggle_approve_article/pending you'll want to handle the reqeust?
If that's the case, why not just do this:
#config/routes.rb
resources :entries, only: [:index, :new, :create] do
put :toggle_approve #-> domain.com/entries/15/toggle_approve
collection do
match :pending, via: [:get, :post] #-> domain.com/entries/pending
match :rejected, via: [:get, :post] #-> domain.com/entries/rejected
match :approved, via: [:get, :post] #-> domain.com/entries/approved
end
end
i had to understand the difference between member and collections. collections are routes additional to a resource (eg, the restful actions). members are extra routes which can perform actions on their the block.
This...
resources :entries, :only => [:index, :new, :create]
namespace :admin do
resources :entries do
get :pending, on: :collection
get :approved, on: :collection
get :rejected, on: :collection
member do
get :toggle_approve_field
get :toggle_reject_field
end
end
end
yielded this rake routes
entries GET /entries(.:format) entries#index
POST /entries(.:format) entries#create
new_entry GET /entries/new(.:format) entries#new
pending_admin_entries GET /admin/entries/pending(.:format) admin/entries#pending
approved_admin_entries GET /admin/entries/approved(.:format) admin/entries#approved
rejected_admin_entries GET /admin/entries/rejected(.:format) admin/entries#rejected
toggle_approve_field_admin_entry GET /admin/entries/:id/toggle_approve_field(.:format) admin/entries#toggle_approve_field
toggle_reject_field_admin_entry GET /admin/entries/:id/toggle_reject_field(.:format) admin/entries#toggle_reject_field
admin_entries GET /admin/entries(.:format) admin/entries#index
POST /admin/entries(.:format) admin/entries#create
new_admin_entry GET /admin/entries/new(.:format) admin/entries#new
edit_admin_entry GET /admin/entries/:id/edit(.:format) admin/entries#edit
admin_entry GET /admin/entries/:id(.:format) admin/entries#show
PATCH /admin/entries/:id(.:format) admin/entries#update
PUT /admin/entries/:id(.:format) admin/entries#update
DELETE /admin/entries/:id(.:format) admin/entries#destroy
It took a lot of faffing around and I not sure I'd be able to do it upon request without more headache but I certainly have a better idea of rails routing all together. Thanks for the help from #RichPeck