Ruby on rails 3 未定义的方法`model#u name';班级:班级

Ruby on rails 3 未定义的方法`model#u name';班级:班级,ruby-on-rails-3,activerecord,activeadmin,Ruby On Rails 3,Activerecord,Activeadmin,我在一个简单的博客中添加评论时遇到了麻烦。使用Haml、Rails 3、ActiveAdmin和Spree NoMethodError in Posts#show Showing /app/views/posts/show.html.haml where line #25 raised: undefined method `model_name' for NilClass:Class Extracted source (around line #25): 22: .

我在一个简单的博客中添加评论时遇到了麻烦。使用Haml、Rails 3、ActiveAdmin和Spree

NoMethodError in Posts#show

Showing /app/views/posts/show.html.haml where line #25 raised:

undefined method `model_name' for NilClass:Class
Extracted source (around line #25):

22:             .comments
23:               %h3 Leave a Reply
24:               / = @comment.inspect
25:               = form_for @comments do |f|
26:                 .row-fluid
27:                   = f.hidden_field :post_id
28:                 .row-fluid

Application Trace | Framework Trace | Full Trace
activemodel (3.2.13) lib/active_model/naming.rb:163:in `model_name_from_record_or_class'
activemodel (3.2.13) lib/active_model/naming.rb:158:in `param_key'
actionpack (3.2.13) lib/action_view/helpers/form_helper.rb:369:in `form_for'
haml (4.0.3) lib/haml/helpers/action_view_mods.rb:146:in `form_for_with_haml'
haml (4.0.3) lib/haml/helpers/action_view_xss_mods.rb:28:in `form_for_with_haml_xss'
app/views/posts/show.html.haml:25:in `block in _1d7665fe0e4edacd3f10a86fba89b0fb'
haml (4.0.3) lib/haml/helpers/action_view_mods.rb:45:in `block in capture_with_haml'
haml (4.0.3) lib/haml/helpers.rb:368:in `call'
haml (4.0.3) lib/haml/helpers.rb:368:in `block in capture_haml'
haml (4.0.3) lib/haml/helpers.rb:617:in `with_haml_buffer'
haml (4.0.3) lib/haml/helpers.rb:364:in `capture_haml'
haml (4.0.3) lib/haml/helpers/xss_mods.rb:61:in `capture_haml_with_haml_xss'
haml (4.0.3) lib/haml/helpers/action_view_mods.rb:45:in `capture_with_haml'
actionpack (3.2.13) lib/action_view/helpers/capture_helper.rb:138:in `content_for'
app/views/posts/show.html.haml:1:in `_1d7665fe0e4edacd3f10a86fba89b0fb'
actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.13) lib/action_view/template.rb:143:in `render'
deface (0.9.1) lib/deface/action_view_extensions.rb:37:in `render'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:18:in `render'
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:110:in `_render_template'
actionpack (3.2.13) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.13) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.13) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/Users/miriam.williams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.13) lib/active_support/callbacks.rb:425:in `_run__1713495783671643053__process_action__2295715638951091725__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__2801646708740260414__call__1745177411958485085__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'

Request

Parameters:

{"title"=>"TEST 2 Post"}
app/controllers/posts_controller.rb

class PostsController < ApplicationController
  def index
    if params[:search]
      @posts = Post.find(
        :all,
        :conditions => ['text LIKE ? or title LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%"], :order => "created_at DESC")
    else
      @posts = Post.find(:all, :order => "created_at DESC")
    end

    @posts_by_month = @posts.group_by { |post| post.created_at.strftime("%B %Y") }
    @recent_posts = Post.order('created_at DESC').limit(5)

    if params[:date]
      date = Time.parse(params[:date])
      date1 = date.beginning_of_month
      date2 = date.end_of_month
      range = date1
      @posts = Post.find(
        :all, :order => "created_at DESC",
        :conditions => ["created_at <= ? AND created_at >= ?", date2, date1])
    end
  end

  def show
    Rails.logger.debug("My object: #{@comment.inspect}")
    title = params[:title]
    @post = Post.where('title' => title).first
    @comment = Comment.new( :post => @post )
  end
End



app/models/post.rb

class Post < ActiveRecord::Base
  attr_accessible :image, :text, :title, :image_file_name
  has_attached_file :image,  :styles => { :thumb => "150x150>", :full => "315x250>" }
  has_many :comments, :dependent => :destroy
end
class CommentsController < ApplicationController
  def create
    @comment = Comment.new(params[:comment])

    if @comment.save
      flash[:notice] = 'Comment was successfully created.'
      redirect_to(@comment.post)
    else
      flash[:notice] = "Error creating comment: #{@comment.errors}"
      redirect_to(@comment.post)
    end
  end

  def new
    @post = Post.find(params[:post_id])
  end
End
App/views/show.html.haml

.row-fluid
            .comments
              %h3 Leave a Reply
              / = @comment.inspect
              = form_for @comment do |f|
                .row-fluid
                  = f.hidden_field :post
                .row-fluid
                  = label_tag(:name, "Name", :id => 'name-label')
                  = f.text_field :name
                .row-fluid
                  = label_tag(:email, "Email", :id => 'email-label')
                  = f.text_field :email
                .row-fluid
                  = label_tag(:website, "Website", :id => 'website-label')
                  = f.text_field :website
                .row-fluid
                  = label_tag(:comment, "Comment", :id => 'comment-label')
                  = f.text_area :comment
                .row-fluid.submit
                  = f.submit("Post Comment", :id => "submit")

您正在使用@comments调用表单_

22:             .comments
23:               %h3 Leave a Reply
24:               / = @comment.inspect
25:               = form_for @comments do |f|
26:                 .row-fluid
27:                   = f.hidden_field :post_id
28:                 .row-fluid
,您尚未在控制器操作中实例化它

def show
    Rails.logger.debug("My object: #{@comment.inspect}")
    title = params[:title]
    @post = Post.where('title' => title).first
    @comment = Comment.new( :post => @post )   end

你应该粘贴你的
posts/show.html.haml
模板。你调用方法名称的变量是nil。所以你需要看看为什么会这样,或者测试nil,只有当它不是时才调用它。@TonyHopkinson
model\u name
在你调用
form\u for
@MarekLIpka app/views/posts/show.html.haml时会在内部调用。这是最后一个片段。@MarekLipka你为什么编辑这个问题?我怎样才能知道你更改了什么?对不起,我粘贴了错误的错误消息。如果您注意到在最后一段代码中,app/views/posts/show.html.haml模板显示我实际上在调用
@comment
。我收到了另一个错误“comments\u path not found”或类似的错误,在尝试修复它时,我更改了该行,得到了上面的错误消息,然后返回到
@comment
。我将关闭这个,然后开始另一个,因为我在这个问题上真的搞砸了。对于显示的错误消息,您的答案是正确的,但是给出了所有的代码片段,这没有帮助。
22:             .comments
23:               %h3 Leave a Reply
24:               / = @comment.inspect
25:               = form_for @comments do |f|
26:                 .row-fluid
27:                   = f.hidden_field :post_id
28:                 .row-fluid
def show
    Rails.logger.debug("My object: #{@comment.inspect}")
    title = params[:title]
    @post = Post.where('title' => title).first
    @comment = Comment.new( :post => @post )   end