Getting a NoMethodError - ruby-on-rails

Im getting this error:
Showing /Users/nelsonkeating/Desktop/imright/app/views/users/new.html.erb where line #4 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #4):
4: <%= form_for(#user) do |f| %>
5: <div class="field">
6: <%= f.label :name %><br />
7: <%= f.text_field :name %>
what do i change to fix this?

#user does not exist then (Look in the error because it is saying that it is a NilClass).
You either want to do:
form_for(User.new) do |f|
or set #user in the controller;
class UsersController
def new
#user = User.new
new
end
I suggest the latter because it's a rule of thumb for MVC to not put model calls in your views.

try to view page
<%= form_for #user, :url=> {:action =>"create"} do |f| %>
in controller
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
flash[:notice] = "Registration successful."
redirect_to(:controller =>"user_sessions", :action =>"new")
else
render :action => 'new'
end
end

That indicates that #user is nil. Perhaps you changed the variable name from #user to something else in your controller and forgot to change it in your view? Maybe this view is being rendered in the create or update action after an error, and the variable is called something besides #user in those actions?

Related

Uninitialized constant controller name

In routes.rb, I used resources nicknames. In nickname_controller, I did:
def index
#nick_name = current_user.nicknames.build
#nick_names = current_user.nicknames.all
end
def create
#nick_name = current_user.nicknames.build(nn_create_param)
if #nick_name.save
flash[:success]= 'Name created'
redirect_to nickname_path
else
flash[:danger]= "Name can't be created"
redirect_to nickname_path
end
end
In index view file,
<%= form_for #nick_name do |f| %>
<%= f.text_field :nickname %>
<%= f.submit 'Submit' %>
<% end %>
When I submit the form, it says uninitialized constant NicknamesController.
Can anyone tell me where the problem is?
The Controller name has to be pluralized.
nicknames_controller.rb
class NicknamesController < ApplicationController
Your controller file name should be nicknames_controller.rb not nickname_controller.rb. And your controller class name should be NicknamesController and not NicknameController.

rails 4 uninitialized constant AuthController::User

I am trying to have a login form in my rails applicaiton but getting an error like
NameError in AuthController#login
uninitialized constant AuthController::User
Here is my view
<%= form_for #users, do |f| %>
<%= f.text_field :name, placeholder: 'Username' %>
<%= end %>
My controller is
def login
##users = User.all I
end
When you use any variable in your view, you must assign/define it in action def of controller page.
You can use for an empty value-
def login
#user = User.new
.......
end
As for login you have no pre assigned values (which is used for edit form), you must use an empty object.
In your controller you would have to define the user and then your form will be called for that particular object:
def login
#user = User.new
.....
end
<%= form_for #user, do |f| %> ....

Rails 4 form_for error: undefined method `model_name' for ActionController::Parameters:Class

I'm trying to make a basic form for creating a user record:
<%= debug(#user) %>
<%= form_for #user, url: {action: "create_user"}, html: {class: "user-create-form"} do |f| %>
<div class="form-group"><%= f.text_field :first_name, class:'form-control' %></div>
<div class="form-group"><%= f.text_field :last_name, class:'form-control' %></div>
<div class="form-group"><%= f.text_field :email, class:'form-control' %></div>
<div class="form-group"><%= f.text_field :password, class:'form-control' %></div>
<%= f.submit "Update" %>
<% end %>
In the controller:
def create_user
#user = params[:user]
#user = User.new if !#user
end
This loads fine and without error, but when I submit the form, I get the following error:
NoMethodError in Admin#create_user
Showing /Applications/MAMP/htdocs/clippo2/app/views/admin/create_user.html.erb where line #6 raised:
undefined method `model_name' for ActionController::Parameters:Class
Extracted source (around line #6):
<%= form_for #user, url: {action: "create_user"}, html: {class: "user-create-form"} do |f| %>
Here's my routes:
get "admin", to: "admin#index"
get "admin/edit_user/:id", to: "admin#edit_user"
patch "admin/edit_user/:id", to: "admin#edit_user"
get "admin/create_user"
post "admin/create_user"
get "admin/delete_user"
get "access/login"
get "access/logout"
post "access/attempt_login"
root :to => 'pages#home'
Extracted source (around line #6):
<%= form_for #user, url: {action: "create_user"}, html: {class: "user-create-form"} do |f| %>
form_for accepts object of ActiveRecord class. But you have made a mistake in controller in create_user action.
#user variable in create_user action is not an object of active_record because you are assigning params[:user] hash to that.
Change your code like this:
def create_user
#user = User.new
end
The create_user requires params[:user'] to function and so there is no need to check for params. A simple modification:
def create_user
#user = User.new(params[:user])
end
Why would you then pass User.new if params are not present? create_user should be a post only route. You require User.new(params[:user]) because if validation fails, you'll pass that object back to the form with errors and existing input.
Change your routes here:
get "admin/create_user" => get "admin/new_user"
Then create a new method in your controller for new_user:
def new_user
#user = User.new
end
You're also building your routes in the wrong way. Use resources:
namespace :admin do
resources :user
end
See this guide for more information.

RoR ruby -- undefined method `model_name' for NilClass:Class

I'm having a trouble with the update on a class.
This is the view:
<div id = "list">
<%= form_for #list do |form| %>
<%= render 'shared/error_messages', object: form.object %>
<div class="list_fields">
<%= form.text_field :name, placeholder:
and this is the controller:
def update
if #list.update_attributes(params[:list])
flash[:success] = "List updated"
else
render 'edit'
end
redirect_to #list
end
The routes are:
resources :lists, only: [:create, :show, :destroy,:edit]
Now the problem is it keeps raising
"undefined method `model_name' for NilClass:Class"
in line 2 ---> <%= form_for #list do |form| %>
And I can't seem to figure out why.
Thanks in advance
Leo
You have to load the #list before you update its attributes.
def update
#list = List.find_by_id(params[:id])
if #list.update_attributes(params[:list])
flash[:success] = "List updated"
else
render 'edit'
end
redirect_to #list
end
And by the way, the problem you see is not caused by your update action but by your edit action that redirects to this view.
You have to load the #list in both actions. In edit action in order to render the view, in update action in order to update the appropriate object.

undefined method `model_name' for NilClass:Class when creating a partial

I'm trying to create a partial template using <%= render "/shopping/coupons/cou" %> . Not really sure where went wrong. Thanks!
This is the error message.
undefined method `model_name' for NilClass:Class
Extracted source (around line #3):
1: <h4> Coupon </h4>
2:
3: <%= form_for(#coupon, :url => shopping_coupon_path(#coupon)) do |f| %>
4: <div class="field">
5: <%= f.label :code %>
6: <%= f.text_field :code %>
this is my coupons controller
class Shopping::CouponsController < Shopping::BaseController
def cou
form_info
end
def create
#coupon = Coupon.find_by_code(params[:coupon][:code])
if #coupon && #coupon.eligible?(session_order) && update_order_coupon_id(#coupon.id)
flash[:notice] = "Successfully added coupon code #{#coupon.code}."
redirect_to shopping_orders_url
else
form_info
flash[:notice] = "Sorry coupon code: #{params[:coupon][:code]} is not valid."
render :action => 'show'
end
end
private
def form_info
#coupon = Coupon.new
end
def update_order_coupon_id(id)
session_order.update_attributes( :coupon_id => id )
end
end
#coupon is nil when the view is being rendered.
The problem might be that <%= render "/shopping/coupons/cou" %> does not go through the cou action in the controller thus form_info method does not execute and #coupon does not get assigned a value.
You have to set #coupon in the action which renders the main view (the one which has the <%= render "/shopping/coupons/cou" %> in it).

Resources