ROR: undefined method ` ' for nil:NilClass
14,390
Try this
<% @results.each do |r| %>
<%= r.fname %>
<%= r.lname %>
<% end %>
It looks like you did a simple typo.
Author by
Niao
Updated on June 04, 2022Comments
-
Niao almost 2 years
Environment: Rails 3.0.1, MySQL
I have a Users table, and I want to search for users by city.I have the following code in users_controller:
def output @results = User.select(:fname, :lname).where(['city = ?', params[:text1]]).all
output.html.erb in View:
<% @results.each do |r| %> <%= @r.fname %> <%= @r.lname %> <% end %>
It will show up as undefined method `fname' for nil:NilClass.
However, if I type following in View, it works:your search are <%= @results %>
The output is:
your search are [#<User fname: "adam", lname: "huang">, #<User fname: "eric", lname: "huang">]
The Users table is:
class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.integer :uid t.string :email t.string :password t.string :fname t.string :lname t.string :city t.integer :pid t.timestamps end end