English 中文(简体)
跟踪铁路辅导员和游乐行动控制员:发现的例外情况?
原标题:Followed a Rails tutorial and got Action controller: Exception caught?

http://ruby.railstutorial.org/ Chapters/user-microposts” rel=“nofollow”>railstutorial.org

错误如下:

NoMethodError in Pages#home

Showing /home/alex/apps/sample_app/app/views/shared/_error_messages.html.erb where line #1 raised:

You have a nil object when you didn t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.errors
Extracted source (around line #1):

1: <% if @user.errors.any? %>
2:   <div id="error_explanation">
3:     <h2><%= pluralize(@user.errors.count, "error") %> 
4:         prohibited this <%= object.class.to_s.underscore.humanize.downcase %> user from being saved:</h2>
Trace of template inclusion: app/views/shared/_micropost_form.html.erb, app/views/pages/home.html.erb

Rails.root: /home/alex/apps/sample_app

Application Trace | Framework Trace | Full Trace
app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb__227730281_80967500 
app/views/shared/_micropost_form.html.erb:2:in `block in _app_views_shared__micropost_form_html_erb___680753694_80558240 
app/views/shared/_micropost_form.html.erb:1:in `_app_views_shared__micropost_form_html_erb___680753694_80558240 
app/views/pages/home.html.erb:6:in `_app_views_pages_home_html_erb__571228236_80133470 

_error_messages.html.erb:

<% if @user.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@user.errors.count, "error") %> 
        prohibited this <%= object.class.to_s.underscore.humanize.downcase %> user from being saved:</h2>
    <p>There were problems with the following fields:</p>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

<>strong>home.html.erb:

<% if signed_in? %>
  <table class="front" summary="For signed-in users">
    <tr>
      <td class="main">
        <h1 class="micropost">What s up?</h1>
        <%= render  shared/micropost_form  %>
      </td>
      <td class="sidebar round">
        <%= render  shared/user_info  %>
      </td>
    </tr>
  </table>
<% else %>
  <h1>Sample App</h1>

  <p>
    This is the home page for the
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
  </p>

  <%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
<% end %>

_micropost_form.html.erb:

<%= form_for @micropost do |f| %>
  <%= render  shared/error_messages , :object => f.object %>
  <div class="field">
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

<>strong>microposts_ Controller.html.erb:

class MicropostsController < ApplicationController
  before_filter :authenticate

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_path
    else
      render  pages/home 
    end
  end

  def destroy
  end
end

pages_ Controller.html.erb

class PagesController < ApplicationController
  def home
        @title = "Home"
    @micropost = Micropost.new if signed_in?
  end

  def contact
        @title = "Contact"
  end

    def about
        @title = "About"
    end

  def help
    @title = "Help"
  end
end

我一直在再次检查辅导,一切照样。

任何解决这一问题的建议?

最佳回答

页: 1 页: 1 因此,@user为零。 假设你完全遵循认证科,则进行转让:@user=当值_user

问题回答

@user。

Extracted source (around line #1):

1: <% if @user.errors.any? %>

<代码>nil没有财产<编码>errors,因此提出了例外情况。

我认为,这与页控制器的家事有关。 您能否从第2页上公布该法典?

Edit: Yeah, see you haven t defined a user - @user.

For _error_messages.html.erb, all @users 页: 1

我的复印件是:

<% if object.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(object.errors.count, "error") %> 
        prohibited this <%= object.class.to_s.underscore.humanize.downcase %> 
        from being saved:</h2>
    <p>There were problems with the following fields:</p>
    <ul>
    <% object.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>




相关问题
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: ...

热门标签