簡體   English   中英

Ruby on Rails入門教程中的注釋錯誤

[英]Comment error in getting started ruby on rails tutorial

我通過此處的官方網站上的入門教程來學習ruby on rails。 http://guides.rubyonrails.org/getting_started.html

我唯一更改的是發布網址映射,指向/ post / friendly-url而不是/ post / id

一切順利,直到我嘗試在帖子中添加評論,然后收到以下錯誤。

CommentsController#create中的NoMethodError

undefined method `comments' for nil:NilClass
app/controllers/comments_controller.rb:7:in `create'

這是我的代碼。

/app/controllers/comments_controller.rb

class CommentsController < ApplicationController
    def create
        @post = Post.find_by_friendly(params[:id])
        @comment = @post.comments.create(params[:comment])
        redirect_to post_path(@post)
    end

    def destroy
        @post = Post.find_by_friendly(params[:id])
        @comment = @post.comments.find(params[:id])
        @comment.destroy
        redirect_to post_path(@post)
    end
end

/app/models/comment.rb

class Comment < ActiveRecord::Base
    belongs_to :post

    attr_accessible :body, :commenter

    validates :body,  :presence => true
    validates :commenter,  :presence => true
end

/app/views/comments/_form.html.erb

<%= form_for([@post, @post.comments.build]) do |f| %>

<%= f.label :commenter, "Your Name:" %>
    <%= f.text_field :commenter, :placeholder => "Your Name..." %>
<span class="help-block">What would you like to be called.</span><br/>

<%= f.label :body, "Your Comment:" %>
    <%= f.text_area :body, :size => "60x12", :placeholder => "Your Comment..." %>
<span class="help-block">What's on your mind?</span><br/>

    <%= f.submit %>

<% end %>

/app/views/posts/_form.html.erb

<% @post.tags.build %>
<%= form_for(@post) do |post_form| %>
<legend>Post Form</legend>
<% if @post.errors.any? %>
    <div id="error_explanation">
        <h2 class="text-error"><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
        <ul>
            <% @post.errors.full_messages.each do |msg| %>
                <li class="text-error"><%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

<%= post_form.label :name, "Post Name:" %>
    <%= post_form.text_field :name, :placeholder => "Post Name..." %>
<span class="help-block">The title of the article.</span><br/>

<%= post_form.label :friendly, "Friendly URL:" %>
    <%= post_form.text_field :friendly, :placeholder => "Friendly URL..." %>
<span class="help-block">SEO friendly URL displayed as /posts/post-name.</span><br/>

<%= post_form.label :content, "Post Content:" %>
<%= post_form.text_area :content, :size => "60x12", :placeholder => "Main Content..." %>
<span class="help-block">HTML enabled article content.</span><br/>

<%= post_form.label :excerpt, "Post Excerpt:" %>
    <%= post_form.text_area :excerpt, :placeholder => "Post Excerpt..." %>
<span class="help-block">Description of post for index page. No HTML.</span><br/>

<h2>Tags</h2>
<%= render :partial => 'tags/form',
    :locals => {:form => post_form} %><br/>

    <%= post_form.submit %>

<% end %>

/config/routes.rb

Nullpulse::Application.routes.draw do
    resources :posts do
        resources :comments
    end

    root :to => "home#index"
end

/apps/models/post.rb

class Post < ActiveRecord::Base
    attr_accessible :content, :friendly, :name, :excerpt, :tags_attributes

    validates :name,  :presence => true
    validates :content, :presence => true
    validates :friendly, :presence => true
    validates :excerpt, :presence => true
    validates_format_of :friendly, :with => /^[^ ]+$/

    has_many :comments, :dependent => :destroy
    has_many :tags

    accepts_nested_attributes_for :tags, :allow_destroy => :true,
        :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }

    def to_param
        friendly
    end
end

抱歉,如果信息過多,也請告知我是否有任何遺漏。 我一直在嘗試所有問題,但找不到問題。 提前致謝。

問題是您在@post CommentController#create中的@postnil 這意味着您提供的params[:id]不正確,或者在數據庫中找不到該參數。 我建議檢查日志以查看params[:id]包含哪些內容,並查看其是否與find_by_friendly所期望的相符。

如果params[:id]看起來正確,我將使用rails控制台嘗試Posts.find_by_friendly並傳遞一些值以查看是否適合您。

如果params[:id]值看起來不正確,則您在comments/_form.html.erb form_for()調用可能是錯誤的,請查看友好插件的文檔以了解如何進行正確的調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM