我在简单博客中为帖子添加评论时遇到问题。使用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"}
应用程序/控制器/ 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
应用程序/控制器/ comments_controller.rb
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
应用程序/模型/ comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
attr_accessible :comment, :email, :name, :website
validates :name,:email, :comment, :presence => true
validates :name, length: 2..20
End
的routes.rb
devise_for :admin_users, ActiveAdmin::Devise.config
# This line mounts Spree's routes at the root of your application.
# This means, any requests to URLs such as /products, will go to Spree::ProductsController.
# If you would like to change where this engine is mounted, simply change the :at option to something different.
#
# We ask that you don't use the :as option here, as Spree relies on it being the default of "spree"
mount Spree::Core::Engine, :at => '/products'
root :to => 'pages#index'
get "history" => 'pages#history', :as => :history
get 'about' => 'pages#about', :as => :about
get 'pajama' => 'pages#pajama', :as => :pajama
get 'comfort' => 'pages#comfort', :as => :comfort
get 'contact' => 'pages#contact', :as => :contact
get 'cookbook' => 'pages#cookbook', :as => :cookbook
get 'charity' => 'pages#charity', :as => :charity
get 'retailers' => 'pages#retailers', :as => :retailers
get 'media' => 'pages#media', :as => :media
get 'official-rules' => 'pages#official_rules', :as => :official_rules
get 'terms-privacy' => 'pages#terms_privacy', :as => :terms_privacy
get "posts/archives" => 'posts#show', :as => :archives
match "posts/archives/:year/:month" => "posts#archives", :constraints => { :year => /\d{4}/, :month => /\d{2}/}
post 'messages' => 'messages#create', :as => :messages
resources :posts do
resources :comments
end
ActiveAdmin.routes(self)
End
应用/视图/ 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")
答案 0 :(得分:0)
您使用@comments
调用form_for22: .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