English 中文(简体)
铁路观点 不确定的方法
原标题:Rails View Undefined Method

我的发言背景不多。 我有用户,有1台qui和1台qui,属于用户。 我正试图列举用户已经采取但我有错误的说法。

用户数指数

显示/Users/Daniel/Documents/cis196/CollegeConnection/app/views/users/index.html。 第24号线的位置:

undefined method `userName for nil:NilClass Extracted source (around line #24):

<% @user.each do |u| %>
  <tr>
    <td><h6><%= u.quiz.userName%></h6></td>
    <td><h6><%= u.quiz.q1 %></h6></td>
    <td><% for q in u.quiz.q2 %>
      <% if q != nil %>

铁路: /Users/Daniel/Documents/cis196/CollegeConnection

Application Trace | Framework Trace | Full Trace app/views/users/index.html.erb:24:in block in _app_views_users_index_html_erb__3921348574137420689_70261694365660 app/views/users/index.html.erb:22:in_app_views_users_index_html_erb__3921348574137420689_70261694365660 app/controllers/users_controller.rb:25:in `index

这里有一些改写法。

class User < ActiveRecord::Base

has_one :quiz, :class_name =>  Quiz 

attr_accessible :name, :email, :password, :password_confirmation, :position, :remember_me
validates :name, :presence => true
validates_length_of :name, :maximum=>30
validates :name, :format => { :with => /A[a-zA-Z]+z/,
:message => "Only letters allowed" }
validates_presence_of :position
validates :email, :presence => true
validates :email, :uniqueness => true

scope :college,    where(position:  College )
scope :highschool, where(position:  High School )

end


class Quiz < ActiveRecord::Base
belongs_to :user

validates :q1, :presence => true
validates :q2, :presence => true
validates :q3, :presence => true
serialize :q2
has_many :activities



 end

def create
@quiz = current_user.quizzes.build(params[:quiz])
# @quiz = Quiz.new(params[:quiz])
@quiz.userName=current_user.name
@activities = Activity.all

respond_to do |format|
  if @quiz.save
    format.html { redirect_to @quiz, notice:  Thank You for Taking the Quiz!.  }
    format.json { render json: @quiz, status: :created, location: @quiz }
  else
    format.html { render action: "new" }
    format.json { render json: @quiz.errors, status: :unprocessable_entity }
  end
end
end

<% @user.each do |u| %>
<tr>
  <td><h6><%= u.quiz.userName%></h6></td>
  <td><h6><%= u.quiz.q1 %></h6></td>
  <td><% for q in u.quiz.q2 %>
  <% if q != nil %>
    <h6><%= q  %></h6>
  <% end %>
<% end %>
<td><h6><%= u.quiz.q3 %></h6></td>
<td>X</td>
</tr>
<% end %>

这是对错误的看法。 任何帮助都是巨大的。

最佳回答

你从你们的角度来看, calling状可变的@用户,但你在控制器的任何地方都不会发现,因此,它就没有。

在要求查阅意见书的控制器中,你必须公布这样的变数:

# assuming you ve already did something like
# @quiz = Quiz.find(params[:id]
# in order to retrieve the quiz
@user = @quiz.user

这样就能够提供“用户”。 欲了解更多情况,请查阅积极登记协会指南:

问题回答

I noticed that you are traversing @user, so that means @user is an array object (probably an array of users).

你们必须检查“@user”是否在控制者的行动或观点中得到了适当的初始化。 在这种情况下,用户——控制者——行动“指数”对@用户进行核对。

另外,由于用户众多,请使用@users而不是@user。

我认为,第24条是:

<td><h6><%= u.quiz.userName%></h6></td>

由于这一错误告诉你,“无”方法没有“用户Name”方法,试图将这种方法强加于它的东西必须是零。 因此,在您的用户感化过程中,其中最有可能没有“静态”。

如果你使用废墟,你可以就此一线指出一个突破点,即检查所有价值(或仅仅使用“投入-u.quiz.inspect”将其丢给青少年。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签