Update User Outside `users#edit` Page - ruby-on-rails

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.

Related

Getting error Couldn't find User with 'id'=id

I am learning ruby on rails by making a project web app,
whenever i go to <%= link_to 'My Profile', user_path(:id) %> error Couldn't find User with 'id'=id" is shown...and url is "http://localhost:3000/users/id
If i convert above link to <%= link_to 'My Profile', user_path %>, it works but now all other pages except user's show page give error No route matches {:action=>"show", :controller=>"users"} missing required keys: [:id]....I cant seem to find any solution anywhere...
Here are my configs:
rake routes Prefix Verb URI Pattern Controller#Action
root GET / static#landing
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
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user GET /users/:id(.:format) users#show
_header.html.erb
<%= link_to 'My Profile', user_path %>
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
end
routes.rb
Rails.application.routes.draw do
root 'static#landing'
devise_for :users
resources :users, :only => [:show]
You need to pass the id of a user to user_path, not the symbol :id - so something like
<%= link_to 'My Profile', user_path(#current_user.id) %>
if the user you want to link to is in an instance variable - or since you are using Devise and it provides the current_user helper you might be wanting
<%= link_to 'My Profile', user_path(current_user.id) %>
I think the first thing wrong is this:
<%= link_to 'My Profile', user_path(:id) %>
You are using a symbol, not a variable
<%= link_to 'My Profile', user_path(#id) %>
Would have an actual variable, assuming you have set #id in your controller.
Normally however you would not set the id for a user in a path - it's a recipe for someone to just change the number manually and view someone elses.
Lets assume you set a session variable called :user_id, then you should do something adding a collection route - so no id is sent.
resources :users do
get :show_profile, on: :collection
end
This gets you a path to your show_profile action, called with
link_to 'My Profile', show_profile_user_path
Then in your controller do your user look up from the session - so something like:
#user = User.find_by id: session[:user_id]
I don't know how devises does things, but please don't trust a user to not fiddle with your parameters!

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 4: TypeError no implicit conversion of Symbol into String

I am relatively new to Rails and I encountered what seems to be a common problem but after reading through all the responses, I cannot seem to make any work for my situation.
I have an app that keeps track of loans and users, and am trying to add a payment feature. When I try to reach the payment page I encounter this TypeError: no implicit conversion of Symbol into String. I have tried a bunch of things but nothing seems to work. I'm not sure if its how I am trying to access the loan_id or something else.
The error is encountered in PaymentsController#create
Here is my view:
show.html.erb
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-md-2">
<ul>
<li><strong>Lender</strong>: <%= #loan.lender.username %></li>
<li><strong>Borrower</strong>: <%= #loan.borrower.username %></li>
<li><strong>Amount Remaining</strong>: <%= #loan.amount %></li>
<li><%= link_to "New Payment", loan_payments_path(#loan.id), :method => :post %></li>
</ul>
<section>
</section>
</div>
</div>
<%= link_to 'Edit', edit_loan_path(#loan) %> |
<%= link_to 'Back', loans_path %>
app/controllers/payments_controller
class PaymentsController < ApplicationController
# POST /loans/:loan_id/payments
def create
#loan = Loan.find(params[:loan_id])
#loan.make_payment(payment)
if #load.save
redirect_to #loan, notice: "Payment successful"
else
redirect_to #loan, alert: "Payment count not be processed"
end
end
private
def payment
require(:loan).require(:payment)[:payment]
end
end
routes
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:index, :show]
resources :loans, shallow: :true do
resources :payments, only: :create
end
root 'static_pages#home'
end
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
users GET /users(.:format) users#index
user GET /users/:id(.:format) users#show
loan_payments POST /loans/:loan_id/payments(.:format) payments#create
loans GET /loans(.:format) loans#index
POST /loans(.:format) loans#create
new_loan GET /loans/new(.:format) loans#new
edit_loan GET /loans/:id/edit(.:format) loans#edit
loan GET /loans/:id(.:format) loans#show
PATCH /loans/:id(.:format) loans#update
PUT /loans/:id(.:format) loans#update
DELETE /loans/:id(.:format) loans#destroy
root GET / static_pages#home
Parameters
{"_method"=>"post",
"authenticity_token"=>"VI7rp4N2eHw+tPkY2noD/so9vnGKy/ue06052Vj1f4qidNzgDjZT3aV5v8+XZMs2ZqiUWE0xzAMSQPWltfT6lg==",
"loan_id"=>"5"}
Your problem in this method:
def payment
require(:loan).permit(:payment)[:payment]
end
Fix it to:
def payment
params.permit(:loan_id)
end
Read how to permit params in Rails.
Update:
Now I get the error ActionController::ParameterMissing in
PaymentsController#create param is missing or the value is empty:
loan
this is normal behavior, because your parameters is empty, you sent nothing to the action, except a load_id, to send the params with #load attributes, pass it to the loan_payments_path() helper, like:
<li><%= link_to "New Payment", loan_payments_path(#loan.id, params: { loan_amount: #loan.amount }), :method => :post %></li>
And permit it in controller code:
def payment
params.permit(:loan_id, :loan_amount)
end

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.

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