Undefined method `user_path' in form_for - ruby-on-rails

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.

Related

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!

Update User Outside `users#edit` Page

I am trying to create a simple button that updates the boolean user attribute notify_generator from its default value of false to true when the user submits a form. The form is on my home#construction page:
<% if current_user.notify_generator == false %>
<h1 class="">Want to know when it's ready?</h1>
<%= form_for(#user) do |f| %> <<<<<<ERROR CALLED ON THIS LINE
<%= f.hidden_field :notify_generator, value: true %>
<%= f.submit "Keep Me In The Loop", class: "btn btn-primary" %>
<% end %>
<% else %>
<h1>You're set! We'll let you know when the generator is up and running!</h1>
<% end %>
The home_controller is as follows:
class HomeController < ApplicationController
before_action :historical_only
skip_before_action :historical_only, except: [:history]
before_action :authenticate_user!, only: [:construction, :generator]
def index
end
def history
end
def about
end
def gear
end
def generator
end
def construction
#user = current_user
end
private
def historical_only
unless current_user && ( current_user.access_historical == true || current_user.admin == true )
flash[:error] = "You must be a historical BurnIt member to see those goodies."
render action: "index"
end
end
end
Right now, when I try to access the home#construction page I get undefined method 'user_path' for #<#<Class:0x007fbc236054a0>:0x007fbc24495470>. I think this is symptomatic for trying to update a user from outside the domain of the registrations controller. I'm using the devise gem, if that makes a difference.
Can anyone see where I'm going wrong here?
ADDITIONAL INFORMATION:
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) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
You have 2 choices: You can either either define your own UsersController, or you can use Devise's existing RegistrationsController.
I won't explain how to implement UsersController--that's standard MVC and I'm sure you've done it before.
But if you want to leverage existing devise code, let's take a look at how devise does this here:
<%= form_for(resource, as: resource_name,
url: registration_path(resource_name), html: { method: :put }) do |f| %>
Notice that you need to pass the option url: registration_path(resource_name). In this case, that resolves to user_registration_path, so you can just use url: user_registration_path. The resource part is specific to devise controllers--because you're using #user instead, you don't need the as: resource_name because Rails can infer that the resource name is user from the name of the instance variable (#user).
You will probably need to customize your permitted parameters. See the devise README for how to do this.

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.

redirecting back to index

I'm building a web app that uses devise. Upon signup with :email, :password, and :password confirmation, the new user is redirected to a second form where they will enter social information (such as hobbies, age, location, etc...) right now the form only contains fields for first and last name.
After submitting the second form, the user should be redirected back to the users#index page which is also where users who are signing in are redirected to. I just created the second form, and after submitting I get the following error:
No route matches [POST] "/users/user/edit_profile"
Below are the relevant files. I'm pretty new, does anyone have any ideas about this?
routes.rb
Appname::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
devise_scope :user do
get 'register', to: 'devise/registrations#new'
get 'login', to: 'devise/sessions#new', as: :login
get 'logout', to: 'devise/sessions#destroy', as: :logout
end
resources :users do
member do
get 'edit_profile'
end
end
root to: "home#index"
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/legal', to: 'static_pages#legal', via: 'get'
end
users_controller.rb
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
def new
end
def create
end
def edit
end
def update
#user = User.find(params[:id])
#user.update!(user_params)
redirect_to #user
end
def destroy
end
def edit_profile_user
#user = User.find(params[:id])
redirect_to #user
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :hobbies)
end
end
edit_profile.html.erb
<h2>Tell us about yourself</h2>
<%= form_for(resource, as: resource_name, url: edit_profile_user_path(resource_name)) do |f| %>
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
<div><%= f.submit "Update" %></div>
<% end %>
here are my rake routes:
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) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
register GET /register(.:format) devise/registrations#new
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
edit_profile_user GET /users/:id/edit_profile(.:format) users#edit_profile
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 / home#index
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
help GET /help(.:format) static_pages#help
legal GET /legal(.:format) static_pages#legal
change:
resources :users do
member do
post 'edit_profile'
end
end
use edit_profile_user_path(#user) instead.
In the Devise login-page, the value of resource_name is just a string "user"... you want to be pointing at an actual user's page, for that you need an actual user instance.... which for Devise's login page is what is stored in resource... but after you're logged in, it's stored in current_user.
However, your code seems to indicate that you're putting user into #user - so that's what I'd use.

undefined method _path (NoMethodError)

I'm receiving the following error in my Rails app when I try to access a page that contains a form to create a post. I'm trying to implement a feature similar to Michael Hartl's Micropost feature in his sample app:
NoMethodError in Home#index
undefined method `posts_path' for #<#<Class:0xb5c70744>:0xb60013b8>
Here's the index view page that contains the code to insert the form:
<%= render 'shared/post_form' if user_signed_in? %>
_post_form.html.erb:
<%= form_for(#post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Provide your network with a status update..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
Here is the Home controller:
class HomeController < ApplicationController
before_filter :authenticate_user!
def index
#render :text => "Welcome #{current_user.email}!"
#users = User.all
#post = current_user.posts.build if signed_in?
end
end
I can really use some help in reviewing the code. I'm staring at it and I need someone else to review it for me. I'm new to Rails so please forgive me if I did not provide the necessary information.
Additional information: I'm using the Devise gem to handle user authentication.
Thanks!
EDIT: I added the wrong controller.
EDIT 2:
Routes.rb file:
AppName::Application.routes.draw do
#get "users/index"
#get "users/show"
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users do
member do
get :following, :followers, :posts
end
end
resources :works
resources :relationships, only: [:create, :destroy]
end
EDIT 3: Rake routes
root / home#index
root / home#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
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) 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
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
following_user GET /users/:id/following(.:format) users#following
followers_user GET /users/:id/followers(.:format) users#followers
posts_user GET /users/:id/posts(.:format) users#posts
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
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
works GET /works(.:format) works#index
POST /works(.:format) works#create
new_work GET /works/new(.:format) works#new
edit_work GET /works/:id/edit(.:format) works#edit
work GET /works/:id(.:format) works#show
PUT /works/:id(.:format) works#update
DELETE /works/:id(.:format) works#destroy
relationships POST /relationships(.:format) relationships#create
relationship DELETE /relationships/:id(.:format) relationships#destroy
You need to add resources :posts in your routes.rb file in order for Rails to automatically create the posts_path helper for you.
Adding resources :posts will generate the proper RESTful routes for you to create, delete, update, and fetch posts. Take a look at the Ruby on Rails Guide for routing, specifically this section here on routing and RESTful routes.

Resources