just getting a really weird error and was wondering if anyone could enlighten me as to what is going on.
First of all here is my code:
class UsersController < ApplicationController
def index
list
render("list")
end#end index
def new
#user = User.new
end#end new
def create
#user = User.new(params[:user])
if #page.save
flash[:notice] = "Page Created Successfully!"
redirect_to(:action => 'list')
else
render('new')
end#if else
end#end create
def list
#list = User.order('users.position ASC')
end#end list
def show
#user = User.find(params[:id])
end#end show
def edit
#user = User.find(params[:id])
end#end edit
def update
#user = User.find(params[:id])
if #user.update_attributes(params[:page])
flash[:notice] = "Page updated Successfully"
redirect_to(:action => 'show', :id => #user.id)
else
render('edit')
end#end if else
end#end update
def delete
#user = User.find(params[:id])
end#end delete
def destroy
User.find(params[:id]).destroy
flash[:notice] = "User has been removed"
redirect_to(:action => 'list')
end#end destroy
end#end class
I am getting a type error when I run the server and go to http://localhost:3000/users/new
NameError in UsersController#new
uninitialized constant UsersController::User
Rails.root: C://Documents/Programming/Ruby Files/kccoding
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:7:in 'new'
But I don't see myself trying to do that at all... AND I am getting no line numbers as to where this error is coming from... Any suggestions?
Kelan
EDIT ~~~ I changed the variables to User.<>, but I'm getting an "uninitialized constant UsersController::User" error. It is in whichever method I am trying to call.
I think this is your problem:
render('new')
It should be:
render :action => 'new'
Try that in your new method.
You sure it's not User.new that you want.
def new
#user = User.new
end#end new
Instead of Users.new you need to type:
def new
#user = User.new
end#end new
Check for proper model name everywhere in controller (Users => User)
It seems error doesn't just come from controller code.
Do post you error trace from log files, something can be found from that only.
Related
Im trying to register users on my rails site. When I click the register button on the register page this is what shows up:
ActiveModel::ForbiddenAttributesError in UserController#register
ActiveModel::ForbiddenAttributesError
Here is the code for my user_controller.rb file:
class UserController < ApplicationController
def index
#title = "RailsSpace User Hub"
end
def register
#title = "Register"
if request.post? and params[:user]
#user = User.new(params[:user])
end
if #user.save
flash[:notice] = "User #{#user.screen_name} created!"
redirect_to :action => "index"
end
end
end
It's complaining about line 11: #user = User.new(params[:user]) Im following code from a book so I dont know what's wrong with it.
Does anyone have any suggestions? Thanks for all your help in advance.
You should use strong parameters.
The UserController should look like this:
class UserController < ApplicationController
def index
#title = "RailsSpace User Hub"
end
def register
#title = "Register"
if request.post? and params[:user]
#user = User.new(user_params)
end
if #user.save
flash[:notice] = "User #{#user.screen_name} created!"
redirect_to :action => "index"
end
end
private
def user_params
# Add the user attributes that you sent with post (form) to the permit method.
params.require(:user).permit(:name, :screen_name)
end
end
After a failed validation I would like the user's brower to display the /new action in the URL not the /create action (which replaces /new after every failed validation). Any way to do this with rails?
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to success_path
else
render 'new'
end
end
You could do it with a redirect using the session instead:
def new
if session[:new_user_params].present?
#user = User.new(session[:new_user_params])
#user.valid?
else
#user = User.new
end
end
def create
#user = User.new(params[:user])
if #user.save
session.delete(:new_user_params)
redirect_to success_path
else
session[:new_user_params] = params[:user]
redirect_to action: :new
end
end
User signs up, is redirected to a page to be collected info, pretty straight forward
I for my life can't figure out how to do this
My controller for the user
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def additional_info
#user = User.find session[:user_id]
#user = User.update(user_addinfo)
redirect_to user_path
end
def create
#user = User.new(user_params)
if #user.save
#session[:user_id] = #user.id
#UserMailer.welcome_email(#user).deliver
sign_in #user
redirect_to additional_info_path
flash[:success] = "Welcome to InYourShoes!"
else
render'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def user_addinfo
params.require(:user).permit(:year)
end
end
user_addinfo is the action method that i want to call updating my record on for my additional_info method.
the def create method has commented line that i'm unsure if necessary, particularly the session[:user_id] = #user.id. I was told that i need this in order to keep track of my session, but perhaps someone can debunk this for me, as im following michael hartl's tutorial.
as of right now with this code, rails is giving me a parameter missing in the
params.require(:user).permit(:year) line.
Much help is greatly appreciated. Ive been trying many different things, and cant seem to figure this out
Change your controller code as below:
def additional_info
#user = User.find params[:id] ## Set #user
end
def update
if #user.update(user_addinfo)
redirect_to user_path(#user), notice: 'User was successfully updated.'
else
render action: 'additional_info'
end
end
def create
#user = User.new(user_params)
if #user.save
#session[:user_id] = #user.id
#UserMailer.welcome_email(#user).deliver
sign_in #user
redirect_to additional_info_path(#user) ## Pass #user
flash[:success] = "Welcome to InYourShoes!"
else
render'new'
end
end
and in your routes.rb update the additional_info route as
get 'info/:id' => 'users#additional_info', :as => 'additional_info'
You additional_info action seems to be wrong. You need to pass in the id of the user for whom you are collecting additional information.
def additional_info
#user = User.find params[:id]
#user.update_attributes(user_addinfo)
redirect_to user_path(#user)
end
The line you have commented in your create method:
#session[:user_id] = #user.id
Is what is storing the user id to a session variable and not a param in the url.
You then have this line commented in your additional_info method
#user = User.find session[:user_id]
This is looking up the user by the id that you would have previously stored in the session variable.
At that point the user object would be stored in user
If you need it in your instance variable, make sure to modify the line to be
#user = User.find session[:user_id]
Your user would then be stored in #user and be able to be accessed in the view
I have been stuck on this error for a while now, I've looked around and apparently it is due to too many end 's, but I can't seem to see why, any help guys? thanks.
Here's my code
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end # your missed end here
end
end # another end missed for the class
I have look around and I'm not sure how to fix this problem. I have a undefined method `update_attributes'. I'm thinking it's because #user is not defined. So if I am able to define #user it should be able to fix it. The thing is I don't know how to define #user in order to fix it. If someone could point me in the right direction that would be great.
Users.controller.rb:
def edit
#user = User.find(params[:id])
end
def update
#user.update_attributes(params[:id])
flash[:success] = "Account updated"
sign_in #user
redirect_to #user
else
render 'edit'
end
If you are using restful paths for your resources then update action should be something like this:
def update
#user = User.find(params[:id])
if #user.update_attributes(params[:user])
flash[:success] = "Account updated"
sign_in #user
redirect_to #user
else
render 'edit'
end
end
We essentially first find the user record through the params[:id] and then update the user fields.