Can't change root route rails - ruby-on-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!

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

Rails 4: Edit/Update Threads, Edit/Update Posts

Before I begin yes I know I need a edit and update function in the posts and threads controller, but the issue I have is with the forum_post.user details getting lost in the update and the thread duplicating posts after the update, so I removed the code entirely so I can get help solving the problem by posting the controllers themselves.
But you're going to need the routes, before I post it /forum/ is just a fake route to nest the forum_threads/posts in and does not exist outside of it's scope.
Rake Routes output
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
forum_thread_forum_posts GET /forum/forum_threads/:forum_thread_id/forum_posts(.:format) forum_threads/forum_posts#index
POST /forum/forum_threads/:forum_thread_id/forum_posts(.:format) forum_threads/forum_posts#create
new_forum_thread_forum_post GET /forum/forum_threads/:forum_thread_id/forum_posts/new(.:format) forum_threads/forum_posts#new
edit_forum_thread_forum_post GET /forum/forum_threads/:forum_thread_id/forum_posts/:id/edit(.:format) forum_threads/forum_posts#edit
forum_thread_forum_post GET /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#show
PATCH /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#update
PUT /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#update
DELETE /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#destroy
forum_threads GET /forum/forum_threads(.:format) forum_threads#index
POST /forum/forum_threads(.:format) forum_threads#create
new_forum_thread GET /forum/forum_threads/new(.:format) forum_threads#new
edit_forum_thread GET /forum/forum_threads/:id/edit(.:format) forum_threads#edit
forum_thread GET /forum/forum_threads/:id(.:format) forum_threads#show
PATCH /forum/forum_threads/:id(.:format) forum_threads#update
PUT /forum/forum_threads/:id(.:format) forum_threads#update
DELETE /forum/forum_threads/:id(.:format) forum_threads#destroy
import_users POST /users/import(.:format) users#import
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / forum_threads#index
Routes:
Rails.application.routes.draw do
devise_for :users
scope "/forum" do
resources :forum_threads do
resources :forum_posts, module: :forum_threads
end
end
resources :users do
collection do
post :import
end
end
root 'forum_threads#index'
end
Forum Threads Controller
class ForumThreadsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread, except: [:index, :new, :create]
def index
#q = ForumThread.search(params[:q])
#forum_threads = #q.result(distinct: true)
end
def show
#forum_post = ForumPost.new
end
def new
#forum_thread = ForumThread.new
#forum_thread.forum_posts.new
end
def create
#forum_thread = current_user.forum_threads.new forum_thread_params
#forum_thread.forum_posts.first.user_id = current_user.id
if #forum_thread.save
redirect_to #forum_thread
else
render action: :new
end
end
def destroy
#forum_thread.destroy
redirect_to root_path
end
private
def set_forum_thread
#forum_thread = ForumThread.find(params[:id])
end
def forum_thread_params
params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body])
end
end
Forum Posts Controller
class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread
def create
#forum_post = #forum_thread.forum_posts.new forum_post_params
#forum_post.user = current_user
if #forum_post.save
redirect_to forum_thread_path(#forum_thread, anchor: "forum_post_#{#forum_post.id}"), notice: "Successfully posted!"
else
redirect_to #forum_thread, alert: "Unable to save your post"
end
end
private
def set_forum_thread
#forum_thread = ForumThread.find(params[:forum_thread_id])
end
def forum_post_params
params.require(:forum_post).permit(:body)
end
end
I know the forum edit path for link_to will be edit_forum_thread_path or just correct me if I'm wrong, but it's the posts edit/delete path I need help with since that controller is nested under forum_threads and using the module forum_threads, I originally figured it would be edit_forum_threads_forum_posts_path but that wasn't it either last time I tried before I removed those functions.
It would be edit_forum_thread_forum_post_path based on your rake routes output.

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.

Undefined method `user_path' in form_for

I have User model and Role model.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :role
def has_role?(role_name)
self.role == Role.where(name: role_name).first
end
def add_role(role_name)
role = Role.where(name: role_name).first
self.role = role unless role.blank?
end
end
class Role < ActiveRecord::Base
belongs_to :user
end
For now, my app have 2 user roles; Admin & Member.
What I'm trying to do now is, giving ability to the Admin to change the member's role (from Admin to Member OR from Member to Admin) using select tag.
I have this inside my views/users/_form.html.erb
<div class="col-md-4">
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<p><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</p>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :email %><br>
<%= f.text_field :email, class: "form-control" %>
<%= f.label :role %>
<%= f.select :role_id, options_for_select(Role.all.map{|c| [c.name, c.id]}, f.object.role_id)%>
</div>
<%= f.submit 'Save user', :class => 'btn btn-primary' %>
<%= link_to 'Back', users_path, class: "btn btn-primary" %>
<% end %>
</div>
This is my User controller:
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
#users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/1/edit
def edit
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to users_path, notice: 'user was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'user was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :encrypted_password)
end
end
And this is my routes.rb:
Rails.application.routes.draw do
devise_for :users
root "orders#index"
resources :orders
resources :drinks
resources :foods
# link to change foods/drinks status from 0 (processing) to 1 (done)
get '/orders/:id/update_foods_status', to: 'orders#update_foods_status', as: :update_foods_status
get '/orders/:id/update_drinks_status', to: 'orders#update_drinks_status', as: :update_drinks_status
# kitchen foods orders
get 'kitchen_foods', to: 'kitchen_foods#index'
# kitchen drinks orders
get 'kitchen_drinks', to: 'kitchen_drinks#index'
# list of all users
get 'users', to: 'users#index', as: :all_users
# single user
get 'users/:id', to: 'users#show', as: :single_user
# edit user
get 'users/:id/edit', to: 'users#edit', as: :edit_user
end
UPDATE: This is what I get after run 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
root GET / orders#index
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
new_order GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PATCH /orders/:id(.:format) orders#update
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
drinks GET /drinks(.:format) drinks#index
POST /drinks(.:format) drinks#create
new_drink GET /drinks/new(.:format) drinks#new
edit_drink GET /drinks/:id/edit(.:format) drinks#edit
drink GET /drinks/:id(.:format) drinks#show
PATCH /drinks/:id(.:format) drinks#update
PUT /drinks/:id(.:format) drinks#update
DELETE /drinks/:id(.:format) drinks#destroy
foods GET /foods(.:format) foods#index
POST /foods(.:format) foods#create
new_food GET /foods/new(.:format) foods#new
edit_food GET /foods/:id/edit(.:format) foods#edit
food GET /foods/:id(.:format) foods#show
PATCH /foods/:id(.:format) foods#update
PUT /foods/:id(.:format) foods#update
DELETE /foods/:id(.:format) foods#destroy
update_foods_status GET /orders/:id/update_foods_status(.:format) orders#update_foods_status
update_drinks_status GET /orders/:id/update_drinks_status(.:format) orders#update_drinks_status
kitchen_foods GET /kitchen_foods(.:format) kitchen_foods#index
kitchen_drinks GET /kitchen_drinks(.:format) kitchen_drinks#index
all_users GET /users(.:format) users#index
single_user GET /users/:id(.:format) users#show
edit_user GET /users/:id/edit(.:format) users#edit
My problem:
When I navigate to /users/1/edit, I get this error:
NoMethodError in Users#edit
undefined method `user_path' for #<#<Class:0x007fb702901c88>:0x007fb70098aaf8>
How do I fix this problem?
I found this answer but I couldn't understand it very clearly. Would appreciate a better explaination and answer.
Note: I'm using Devise gem.
You have in your routes file:
devise_for :users
which adds:
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
Now what you want to do is allow admin to edit the users. For this you have defined:
# list of all users
get 'users', to: 'users#index', as: :all_users
# single user
get 'users/:id', to: 'users#show', as: :single_user
# edit user
get 'users/:id/edit', to: 'users#edit', as: :edit_user
which generates:
all_users GET /users(.:format) users#index
single_user GET /users/:id(.:format) users#show
edit_user GET /users/:id/edit(.:format) users#edit
Now in the form you are writing <%= form_for(#user) do |f| %> which by default is searching for user_path. So instead of the different routes you are defining just add:
resources :users
which will generate all the necessary routes you require and map the form to the correct path. This will save a lot of overhead like using only this <%= form_for(#user) do |f| %> will take you to the create action when the user is a new object and it will take you to the update action if the user is a persisted object.
Or if you need to user your routes then you need to do:
<%= form_for(#user, url: edit_user_path, method: :get) do |f| %>
But it always a bad idea to use GET method in form submission. These links might help you:
When should I use GET or POST method? What's the difference between them?
When do you use POST and when do you use GET?
Update:
As you need to assign a single role to the user from multiple it is better to use a select box like this:
f.collection_select(:role_id, Role.all, :id, :name)
Assuming that the user table has attribute role_id and the role table has attribute name. So this will allow you to assign a role to user. What you are printing is user.role which will always print the association instead of the role name.
Hope this helps.
Try using edit_user_path instead of user_path if you want to link to the edit page. If you take a look at your routes.rb file there is no route for user_path.

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.

Resources