redirecting back to index - ruby-on-rails

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.

Related

display user's profile fail

Please read at the very bottom, I edited my post, I still need help.
regards
I am using devise to authenticate the users and admin with (admin: true). As an admin I want to visit the users profile's pages but I always arrive on my own profile ( as the current_user). I don't know how to do...
Users could see others users profile too
Thanks for your help
users/index.html.slim
.container
h1 All the users
.row
table.board
thead
tr
th First Name
th Last Name
th Email Address
th Action on User
hr
tbody.board
-#users.each do |user|
.row
.col-xs-3
= user.first_name
.col-xs-3
= user.last_name
.col-xs-3
= user.email
.col-xs-1
#The problem is this link
= link_to 'View', user_path(user.id), class:'btn btn-success'
.col-xs-1
= link_to 'Remove', user_path(user), class:'btn btn-danger', method: :delete, data: {confirm: "Are you sure?"}
hr
users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!
def show
#binding.pry
##user = User.find(current_user)
##user.id = User.find(params[:id])
#user = User.find(user_params[:id]) || current_user
#tutos= Tuto.all
end
def index
if current_user.admin == true
#users = User.all
else
redirect_to root_path
end
end
def destroy
#user = User.find(params[:id])
#user.destroy
flash[:success] = "User was successfully deleted"
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :id)
end
end
The routes
#edited
Rails.application.routes.draw do
namespace :users do
resources :tutos
end
resources :tutos, only: [:show]
resources :tutos do
member do
put "like", to: "tutos#upvote"
end
end
get "/register", to: "devise/registrations#new", as: :register
get "/login", to: "devise/sessions#new", as: :login
get "/logout", to: "devise/sessions#destroy", as: :logout
get "/account", to: "users#show", as: :account
get "/login" , to: "devise/sessions#new", as: :new_user_session
post "/login" , to: "devise/sessions#create", as: :user_session
delete "/logout" , to: "devise/sessions#destroy", as: :destroy_user_session
devise_for :users, skip: [:sessions]
resources :users
root "home#landing"
end
edit rake routes gives :
$ rake routes
Prefix Verb URI Pattern Controller#Action
users_tutos GET /users/tutos(.:format) users/tutos#index
POST /users/tutos(.:format) users/tutos#create
new_users_tuto GET /users/tutos/new(.:format) users/tutos#new
edit_users_tuto GET /users/tutos/:id/edit(.:format) users/tutos#edit
users_tuto GET /users/tutos/:id(.:format) users/tutos#show
PATCH /users/tutos/:id(.:format) users/tutos#update
PUT /users/tutos/:id(.:format) users/tutos#update
DELETE /users/tutos/:id(.:format) users/tutos#destroy
like_tuto PUT /tutos/:id/like(.:format) tutos#upvote
tutos GET /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
new_tuto GET /tutos/new(.:format) tutos#new
edit_tuto GET /tutos/:id/edit(.:format) tutos#edit
tuto GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
register GET /register(.:format) devise/registrations#new
login GET /login(.:format) devise/sessions#new
logout GET /logout(.: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
account GET /account(.:format) users#show
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
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
GET /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
root GET / home#landing
edit
after the last edits I still have a problem....
when I try to go on the account_path I have this error
Last edit
Just to remind you, I am using devise:
As a user logged in, if I want to see my own profile, I use
account_path (and this work well)
The link for visiting a user's profile page looks like this:
= link_to 'View', user_path(user)
but it looks like it point exactly like: account_path. ( So on my profile, not on the user I want to visit)
I am not sure what to use in my controller, if I use #user = User.find(user_params[:id]) || current_user or ##user = User.find(user_params[:id])
I have the following error:
ActionController::ParameterMissing in UsersController#show
param is missing or the value is empty: user
If I use #user = User.find(current_user).
I am redirected on my own profile each time....
def show
#binding.pry
##user = User.find(current_user)
##user = User.find(user_params[:id])
#user = User.find(user_params[:id]) || current_user
#tutos = Tuto.all
end
This will set #user to current user or to requested user for admins
#user = current_user.admin? ? User.find(params[:id]) : current_user
In #show you could something like:
# Assuming params[:id] is the ID of the user's profile you're trying to view
def show
user_id = current_user.admin? ? params[:id] : current_user.id
#user = User.find(user_id)
end
The problem is in your route file remove 'as user do' blocks it makes some bizzare things :
user GET /users/:id(.:format) users#show
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
Do you want to use the account_path to view another user's profile, or could you go to /users/2 for the user with id of 2? That uses the show action in the controller and routes to the user path using the relevant id?

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.

User not being redirected to user show page upon devise login

After logging in or signing up, the user should be redirected to their individual show page. I'm using some code from Hartl's guide, but I can't seem to get it to work:
Controller:
class UsersController < ApplicationController
before_filter :authenticate_user!
def show
#user = User.find(params[:id])
end
def new
end
def create
#user = User.new(user_params)
if #user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
redirect_to login_path
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
Routes:
devise_for :users, :path => '', :path_names => { :sign_in => 'login', :sign_out => 'logout',
:password => 'password', :confirmation => 'verification',
:unlock => 'unblock', :registration => 'signup',
:sign_up => 'new' }
devise_scope :user do
get 'login', to: 'devise/sessions#new'
get "logout", :to => "devise/sessions#destroy"
get "signup", :to => "devise/registrations#new"
get "password", :to => "devise/passwords#new"
match 'users/secret', to: "devise/passwords#create", via: :post
match 'sessions/user', to: 'devise/sessions#create', via: :post
match 'users/signup', to: 'devise/registrations#create', via: :post
end
resources :users
resources :sessions
root 'home#index'
My /views/users/show.html.erb is just a simple page, just until I can get the redirect working.
Edit:
rake routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
user_password POST /password(.:format) devise/passwords#create
new_user_password GET /password/new(.:format) devise/passwords#new
edit_user_password GET /password/edit(.:format) devise/passwords#edit
PATCH /password(.:format) devise/passwords#update
PUT /password(.:format) devise/passwords#update
cancel_user_registration GET /signup/cancel(.:format) devise/registrations#cancel
user_registration POST /signup(.:format) devise/registrations#create
new_user_registration GET /signup/new(.:format) devise/registrations#new
edit_user_registration GET /signup/edit(.:format) devise/registrations#edit
PATCH /signup(.:format) devise/registrations#update
PUT /signup(.:format) devise/registrations#update
DELETE /signup(.:format) devise/registrations#destroy
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
signup GET /signup(.:format) devise/registrations#new
password GET /password(.:format) devise/passwords#new
users_secret POST /users/secret(.:format) devise/passwords#create
sessions_user POST /sessions/user(.:format) devise/sessions#create
users_signup POST /users/signup(.:format) devise/registrations#create
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
sessions GET /sessions(.:format) sessions#index
POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
edit_session GET /sessions/:id/edit(.:format) sessions#edit
session GET /sessions/:id(.:format) sessions#show
PATCH /sessions/:id(.:format) sessions#update
PUT /sessions/:id(.:format) sessions#update
DELETE /sessions/:id(.:format) sessions#destroy
root GET / home#index
The problem is in the last line of your rake routes output:
root GET / home#index
which points to this line in your routes.rb
root 'home#index'
You should remove that line in routes.rb and replace it with whatever you want your root path to be.

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.

Rails Mongoid form_for Not Using the Correct Verb

I've got an embedded resource inside of a singular resource (/profile/workout) and am having some problems with the form_for helper.
I've defined the following helper, since profile is just based off the current_user (just redirects to the right url):
def workout_path(*args)
profile_workout_path(*args)
end
I've got the following model:
class Workout
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
embedded_in :user
embeds_many :movements
accepts_nested_attributes_for :movements
end
controller:
def new
#workout = current_user.workouts.build
#workout.movements.build
end
routes:
ComposerDelete::Application.routes.draw do
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users do
end
resource :profile do
resources :workouts
end
end
and form
= form_for #workout do |f|
%fieldset
= f.label :name
= f.text_field :name
= f.fields_for :movements do |builder|
= render "movement_fields", f: builder
= link_to_add_fields "Add Movement", f, :movements
= f.submit "Create"
When I visit the url: http://localhost:3500/profile/workouts/50b99b70f0f800cd53000002/edit, the form has the following header:
<form accept-charset="UTF-8" action="/profile/workouts/50b99b70f0f800cd53000002" class="edit_workout" id="edit_workout_50b99b70f0f800cd53000002" method="post">
It gets the right id (edit_<model>_<id>), but the wrong method (post, should be put), also, the submit button says Create instead of Update. The form works correctly, and updates the workout.
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
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
profile_workouts GET /profile/workouts(.:format) workouts#index
POST /profile/workouts(.:format) workouts#create
new_profile_workout GET /profile/workouts/new(.:format) workouts#new
edit_profile_workout GET /profile/workouts/:id/edit(.:format) workouts#edit
profile_workout GET /profile/workouts/:id(.:format) workouts#show
PUT /profile/workouts/:id(.:format) workouts#update
DELETE /profile/workouts/:id(.:format) workouts#destroy
profile POST /profile(.:format) profiles#create
new_profile GET /profile/new(.:format) profiles#new
edit_profile GET /profile/edit(.:format) profiles#edit
GET /profile(.:format) profiles#show
PUT /profile(.:format) profiles#update
DELETE /profile(.:format) profiles#destroy
The method will always be POST, not PUT - your browser does not support PUT natively, so Rails hacks around this by using a hidden form field inside the form with _method=PUT. See here for some documentation on this.
also, your own code says = f.submit "Create" so it's no wonder the button says 'Create'.

Resources