Why didn't change url in browser field? - ruby-on-rails

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.

Related

Passing nested params from create in redirect to show of nested controller

I have a schema like:
Company belongs to Users (devise)
Quote belongs to Company
Employees belong to Company
I have a simple_form_for using cocoon to create Company & Quote & Employees from the create action in the Company controller, all objects being created just fine by the create method. But on .save of these objects I am trying to redirect to Quotes#show of the quote created by Companies#create but I'm having trouble getting the quote_id over to Quotes#show.
Can you help me understand how to get the right params over to succesfully redirect? Thanks.
companies.rb
class CompaniesController < ApplicationController
before_action :authenticate_user!, only: [ :new, :create, :edit, :update, :destroy ]
def new
#company = Company.new
#company.quotes.build
#company.employees.build
end
def create
#company = current_user.companies.new(company_params)
if #company.save
redirect_to company_quote_url(#company.id), notice: 'Quote request created'
else
render :new
end
end
private
def company_params
params.require(:company).permit(:co_name, :co_number, :postcode, :industry,
:quotes_attributes => [:id, :lives_overseas, :payment_frequency],
:employees_attributes => [:id, :first_name, :last_name, :email, :gender, :date_of_birth, :salary, :_destroy] )
end
end
quotes.rb
class QuotesController < ApplicationController
def show
#quote = #company.quotes.find(params)
end
end
routes
quotes_show GET /quotes/show(.:format) quotes#show
quotes_index GET /quotes/index(.:format) quotes#index
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
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
company_quotes GET /companies/:company_id/quotes(.:format) quotes#index
company_quote GET /companies/:company_id/quotes/:id(.:format) quotes#show
company_employees GET /companies/:company_id/employees(.:format) employees#index
company_employee GET /companies/:company_id/employees/:id(.:format) employees#show
companies GET /companies(.:format) companies#index
POST /companies(.:format) companies#create
new_company GET /companies/new(.:format) companies#new
edit_company GET /companies/:id/edit(.:format) companies#edit
company GET /companies/:id(.:format) companies#show
PATCH /companies/:id(.:format) companies#update
PUT /companies/:id(.:format) companies#update
DELETE /companies/:id(.:format) companies#destroy
root GET / companies#new
error message
No route matches {:action=>"show", :company_id=>65, :controller=>"quotes"} missing required keys: [:id]
I can't see how to get the quote_id route accross from Companies#create over to Quotes#show. When I run the redirect like; redirect_to company_quote_url(company_params) the error shows the header params being passed like this, i.e. this is what's available;
No route matches {:action=>"show", "co_name"=>"acme1", "co_number"=>"12345678", :controller=>"quotes",
"employees_attributes"=>{"0"=>{"first_name"=>"brian", "last_name"=>"blessed", "email"=>"brian#test.com", "gender"=>"m", "date_of_birth(1i)"=>"2001", "date_of_birth(2i)"=>"6", "date_of_birth(3i)"=>"28", "salary"=>"10000", "_destroy"=>"false"}}, "industry"=>"financial_services", "postcode"=>"al8 8ba",
"quotes_attributes"=>{"0"=>{"lives_overseas"=>"true", "payment_frequency"=>"annually"}}} missing required keys: [:company_id, :id]
I've played and failed with different variations of;
(params[:company][:quote_attributes[:id]])
(#company.quote.id)
yet i just can't seem to get it right, can anyone help please. Thanks
Since company has_many quotes, you should track the latest quote for that company and redirect to that show view of the quote.
def create
#company = current_user.companies.new(company_params)
if #company.save
#quote = #company.quotes.last
redirect_to company_quote_url(#company,#quote), notice: 'Quote request created'
else
render :new
end
end
last will give you the latest record with the help of created_at
Also you should tweak your quotes#show like below else it will error out.
def show
#company = Company.find(params[:company_id])
#quote = #company.quotes.find(params[:id])
end

Can't change root route rails

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!

RoR link_to a different file

I made a file in my posts view called lists.html.erb, and it lists all the different posts. In my index.html.erb, it lists up to 5 posts. The button is supposed to link to multiple posts, but I get an error saying that I have an undefined local variable or method. I am trying:
<%= link_to "All Posts", lists_path %>
and
<%= link_to "All Posts", lists %>
For a question
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
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / posts#index
GET /*path(.:format) redirect(301, /)
Posts_controller.rb
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
def index
#posts = Post.all.order('created_at DESC')
end
def new
#post = Post.new
if #post.save
redirect_to #post
else
render 'new'
end
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to #post, notice: "Post was saved"
else
render 'new', notice: "I could not save the post. Call me for help if it keeps happening"
end
end
def show
##post = Post.find(params[:id])
end
def edit
##post = Post.find(params[:id])
end
def lists
#posts = Post.all.order('created_at DESC')
end
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title, :body))
redirect_to #post
else
render 'edit'
end
end
def destroy
##post = Post.find(params[:id])
if #post.destroy
redirect_to root_path
else
redirect_to post_path, notice: "I couldn't be deleted for some reason. Try again or contact me"
end
end
private
def post_params
params.require(:post).permit(:title, :body, :image, :slug)
end
def find_post
#post = Post.friendly.find(params[:id])
end
end
You have the method in your controller, but you didn't put the route for it:
def lists
#posts = Post.all.order('created_at DESC')
end
You can add a simple route, like
get 'lists' => 'posts#lists', :as => :lists
And if you run rake routes it will show this new route - lists_path so <%= link_to "All Posts", lists_path %> will work.

rails & devise, route to home#show after login using after_sign_in_path_for

After sign-in, I want to route to my home#index, but instead, it generates a /home/:id request and hence routed to home#show.
Can someone help and see, is it possible for me to route to index after clicking the sign in button.
Thank you so much.
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_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
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
homes GET /homes(.:format) homes#index
POST /homes(.:format) homes#create
new_home GET /homes/new(.:format) homes#new
edit_home GET /homes/:id/edit(.:format) homes#edit
home GET /homes/:id(.:format) homes#show
PATCH /homes/:id(.:format) homes#update
PUT /homes/:id(.:format) homes#update
DELETE /homes/:id(.:format) homes#destroy
root GET / devise/sessions#new
ApplicationController.rb
class ApplicationController < ActionController::Base
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end
protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
home_path(resource)
end
def after_sign_out_path_for(resource)
new_user_session_path()
end
end
sessions/new.html.erb
<%= form_for(resource, as: resource_name, url: session_path(resource_name), :html => {:class=>'form-
signin'}) do |f| %>
<h2 class="form-signin-heading" style="text-align:center">Log in</h2>
<%= f.email_field :email, autofocus: true,:class=>"form-control", :type=>"email", :placeholder=>"Enter email" %>
<%= f.password_field :password, autocomplete: "off", :class=>"form-control", :type=>"password", :placeholder=>"Password"%>
<%= f.submit "Submit", :class=>"btn btn-primary btn-block" %>
<%= render "devise/shared/links" %>
<% end %>
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :posts
resources :homes
# routing to the login page
devise_scope :user do
root :to => 'devise/sessions#new'
end
HomesController.rb
class HomesController < ApplicationController
layout "loginpage"
def index
end
def show
#heading = "My Home"
end
def new
#home = Home.new
respond_with(#home)
end
def edit
end
def create
#home = Home.new(home_params)
#home.save
respond_with(#home)
end
def update
#home.update(home_params)
respond_with(#home)
end
def destroy
#home.destroy
respond_with(#home)
end
private
def set_home
#home = Home.find(params[:id])
end
def home_params
params[:home]
end
end
You want after signin redirect to homes#index but your path on after_sign_in_path_for(resource) method have home_path(resource) it will redirect to homes#show
look at this (your output of rake routes)
-> homes GET /homes(.:format) homes#index
-> home GET /homes/:id(.:format) homes#show
homes_path is named helper for path /homes with controller#action is homes#index
home_path(id) is named helped for path /home/:id with controller#action is homes#show
http://guides.rubyonrails.org/routing.html#path-and-url-helpers
Try this :
If you have multiple device models
class ApplicationController < ActionController::Base
private
def after_sign_in_path_for(resource)
if resource.is_a?(YourDeviceModel)
homes_path
else
another_path
end
end
end
If you have single device model
class ApplicationController < ActionController::Base
private
def after_sign_in_path_for(resource)
homes_path
end
end
For your question on comment
Q: without the s, it will route to show method?
It may not always be so, You have resources :homes on your routes.rb it can generate routing default by rails. You can read this http://guides.rubyonrails.org/routing.html
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
home_path
end
end
config/routes.rb
Rails.application.routes.draw do
get 'home', to: "home#index", as: :home
end
app/controllers/home_controller.rb
class HomeController < ActionController::Base
def index
end
end
When you specify a static route in Rails, you can just say which HTTP action you want to use, followed by the controller#action you want to forward the request to, followed by the _path name you want to set.
In your case you specified that you wanted to send the signed in user to the home_path so we set home_path via the as: param.

routing with non-restful actions issue rails

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

Resources