My problem is that if I want to test the edit form I always get this exception..
Can you help me figure out the problem?
This is the error message:
undefined method `model_name' for NilClass:Class
Extracted source (around line #5):
2: <div class="row">
3: <div class="box">
4: <span id="logo">Azubiware 2.0</span><br><br>
5: <%= form_for(#bsinfo) do |f| %>
6: <% #basedate = Date.new(#bsinfo.year) %>
7: <% #basedate = #basedate.beginning_of_year %>
8: <% #basedate = #basedate.beginning_of_week %>
I have the same form going with my users table and this works properly...
class BsinfosController < ApplicationController
def index
#title = "Verwaltung Abwesehnheiten"
end
def new
#title = "Sign up"
#bsinfo = Bsinfo.new
end
def show
#bsinfo = Bsinfo.find(params[:id])
#title = #bsinfo.year
end
def create
#bsinfo = Bsinfo.new(params[:bsinfo])
if #bsinfo.save
flash[:success] = "Schedule successfull created"
redirect_to bsinfos_path
else
render 'new'
end
end
def edit
#title = "Settings"
end
def update
if #bsinfo.update_attributes(params[:bsinfo])
flash[:success] = "Profile successfull updated"
redirect_to #bsinfo
else
render 'edit'
end
end
def destroy
Bsinfo.find(params[:id]).destroy
flash[:success] = "Scheduel destroyed"
redirect_to bsinfos_path
end
end
And the link to the edit form is like
<% #bs = Bsinfo.all %>
<% #bs.each do |bsall| %>
<%= link_to "#{bsall.name}", edit_bsinfo_path(bsall), :class => "btn" %>
<% end %>
The url appears like
localhost:3000/bsinfos/17/edit
Whatever model your form is based on is being returned as nil. Make sure the #var in the controller that the form is based on is actually set to the instance of the object you want.
The snippet of code:
form_for(#bsinfo)
is generating your error most likely because #bsinfo is nil.
I'd recommend looking at your controller code and checking for the conditions under which #bsinfo can be nil.
Related
I am a begginer in Rails, im following code from a book and i am trying stuff to see if it breaks/works, anyway heres my UserControllers classUserController
class UsersController < ApplicationController
def new
#user = User.new
end
def edit
#user = User.find(params[:id])
end
def show
#user = User.find(params[:id])
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to #user, :notice => 'Cadastro realizado'
else
render :new
end
end
end
And heres my show.html.erb
<p id="notice"><%=notice%></p>
<h2>Perfil: <%=#user.full_name %></h2>
<ul>
<li>Localização: <%= #user.location %> </li>
<li>Bio: <%= #user.bio %></li>
</ul>
<%= link_to 'Editar Perfil', edit_user_path(#user) %>
<%= link_to 'Mostrar Perfil', show_user_path(#user) %>
My problem is in the last line, when i try to acess this page i get a NomethodError,i am trying to understand why, why can i just change that to #user and the page works?
Try:
<%= link_to 'Mostrar Perfil', user_path(#user) %>
or even just
<%= link_to 'Mostrar Perfil', #user %>
In order to see how to name the routes, open a console and run
rake routes
So I want to update an user in User table on the page and then when it's done, redirect to that user's page.
Whenever I do this, I get this error:
undefined method `name' for nil:NilClass
<% provide(:title, #user.name) %>
<p>
<%= #user.name %>
</p>
This is how my update looks like:
def update
#user = User.find(params[:id])
if #user.update_attributes(user_params)
flash[:success] = "Update successful"
redirect_to #user
else
render 'edit'
end
end
The page I redirect to:
<% provide(:title, #user.name) %>
<p>
<%= #user.name %>
</p>
What am I doing wrong here? Will provide more info if needed.
Edit:
Show action:
def show
if signed_in?
#user = User.find(params[:id])
end
end
Here is my Edited details:
I have my controller like,
class Enr::Rds::SurvRdsapXrefsController < Enr::Controller
def index
#enr_rds_surv_rdsap_xrefs = Enr::Rds::SurvRdsapXref.paginate(page: params[:page])
end
def show
end
def new
#enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.new
end
def edit
#enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.find(params[:id])
end
def create
#enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.new(params[:enr_rds_surv_rdsap_xref])
respond_to do |format|
if #enr_rds_surv_rdsap_xref.save
format.html { redirect_to :enr_rds_surv_rdsap_xrefs, notice: "Survey link was successfully created." }
format.js
format.json { render json: #enr_rds_surv_rdsap_xref, status: :created, location: #enr_rds_surv_rdsap_xref }
else
format.html { render action: "new" }
format.js
format.json { render json: #enr_rds_surv_rdsap_xref.errors, status: :unprocessable_entity }
end
end
end
def update
end
def destroy
end
end
Here is my view form like
<%= form_for(#enr_rds_surv_rdsap_xref, :remote => true) do |f| %>
<% if #enr_rds_surv_rdsap_xref.errors.any? %>
<div id="error_explanation">
<div class="validate">
The form contains <%= pluralize#enr_rds_surv_rdsap_xref.errors.count, "error") %>.
</div>
<ul>
<% #enr_rds_surv_rdsap_xref.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="control-group">
<%= f.label :section %><br />
<%= f.text_field :section %>
</div>
<%= f.submit %>
<% end %>
When i click the index page to create a new link, the index page showing error like
NoMethodError in Enr/rds/surv_rdsap_xrefs#index
undefined method `errors' for nil:NilClass
Thanks for the suppport and please suggest me to rectify the error. I am new to ROR. Thanks
Your error reveals that the rendering of the index template is causing the error, which means you're rendering the form for the new survey (the code snippet above) in the index template. This is fine, but if you're going to do that, you'll have to instantiate a new survey in index, as well as in new.
At the simplest, you could just copy the code in new to index:
def index
#enr_rds_surv_rdsap_xrefs = Enr::Rds::SurvRdsapXref.paginate(page: params[:page])
#enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.new
end
def new
#enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.new
end
To keep your code a bit DRYer you might change where the new instance is created. A pattern you'll often see is something similar to:
before_filter :build_record, :only => [:new, :index]
protected
def build_record
#survey = YourSurvey.new
end
This way you don't even need to write the new/index methods if you don't have any other logic.
Do you also set #survey in the new action in your controller? The error means that when the view is rendered #survey is nil, so there must be a problem with setting that instance variable.
Do you get the error when you go to the 'new' view or when you try to submit the form (create)?
The form contains <%= pluralize#enr_rds_surv_rdsap_xref.errors.count, "error") %>
This line of the code is the problem. You are lacking a "(" between the pluralize and the #enr...
Explained: RoR thinks that the object is: pluralize#enr... instead of the # All alone, and he has no errors for this kind of object.
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).
I used this code in my "users" views and had no trouble: <% if current_user.admin? %>. But using it in a set of views associated with a different controller throws up the "No method Error."
Background: the app allows admins to create scavenger hunts. Admins should be able to delete hunts. I thought I knew how to configure everything, but apparently, I'm missing something. Here's my code:
controller.rb
class HuntsController < ApplicationController
def index
#title = "All Hunts"
#hunts = Hunt.order("name ASC")
end
def show
#hunt = Hunt.find(params[:id])
#title = #hunt.name
end
def new
#hunt = Hunt.new
#title = "New Hunt"
end
def create
#hunt = Hunt.new(params[:hunt])
if #hunt.save
flash[:success] = "Hunt created!"
redirect_to hunts
else
#title = "New Hunt"
render 'new'
end
end
def edit
#hunt = Hunt.find(params[:id])
#title = "Edit hunt"
end
def delete
Hunt.find(params[:id]).destroy
flash[:success] = "Hunt destroyed."
redirect_to index
end
end
Views/Index.html.erb
<h1>All Hunts</h1>
<ul>
<% #hunts.each do |hunt| %>
<%= render hunt %>
<% end %>
</ul>
<%= link_to( "Create New Hunt", '/hunts/new') %>
Views/_hunt.html.erb
<li>
<%= link_to hunt.name, hunt %>
<% if current_user.admin? %>
<%= link_to "delete", hunt, :method => :delete, :confirm => "You sure?",
:title => "Delete #{hunt.name}" %>
<% end %>
</li>
Error Message when trying to head to /hunts:
NoMethodError in Hunts#index
Showing ...../app/views/hunts/_hunt.html.erb where line #3 raised:
undefined method `admin?' for nil:NilClass
current_user is nil, and thus does not know how to respond to admin?. Either ensure that current_user is always a user instance, or check that it's not nil.
In Ruby 2.3+, one can use the “safe navigation” operator (&.):
if current_user&.admin?
In Ruby 2.2 and earlier, instead use boolean short-circuiting:
if current_user && current_user.admin?
Note that ActiveSupport has try, but that has different behavior which will potentially hide bugs. For similar behavior, use try! instead.
Getting "undefined method _____ for nil:NilClass" is a very common occurrence in Ruby, so get used to it happening often :).
You have to sign in the user in order to instantiate current_user. If you are using devise, use:
class HuntsController < ApplicationController
before_filter :authenticate_user!
def index
...
end
.......
end
in your controller. And make sure that .admin? method is defined in your User model.