0

我的问题与前一个问题有些相似,但就我而言,我更像是一个初学者,而且我的服务器在Ruby-on-Rails 入门指南的第一页上出现故障。

基本上,该指南将引导我创建一个带有评论表单的简单博客文章应用程序。按照指南,我可以创建博客文章,并呈现评论表单。评论“显示”区域甚至呈现,它显示正确数量的评论,但存储在数据库中的值对于“评论者”和“正文”字段都是“nil”。

我直接从指南中复制了代码,因此发布它可能不会增加任何价值。这是我development.log为有趣的事件添加的空间,因此您可以看到我所看到的:

    Started POST "/posts/1/comments" for 127.0.0.1 at 2012-12-17 06:50:32 -0600
Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>
"YuuqmOYgsUHoHTYKBDjeE+zkYSAfWbsB4LcUz6btUkU=",

  "comment"=>{"commenter"=>"just me", "body"=>"Yes"},

 "commit"=>"Create Comment", "post_id"=>"1"}
  [1m[35mPost Load (0.1ms)[0m  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id",     "1"]]
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35mSQL (0.5ms)[0m  INSERT INTO "comments" ("body", "commenter", "created_at",
"post_id",     "updated_at") VALUES (?, ?, ?, ?, ?)

 [["body", nil], ["commenter", nil],

 ["created_at", Mon, 17 Dec     2012 12:50:32 UTC +00:00], ["post_id", 1],
 ["updated_at", Mon, 17      Dec     2012 12:50:32 UTC +00:00]]
  [1m[36m (5.8ms)[0m  [1mcommit transaction[0m
Redirected to http://localhost:3000/posts/1
Completed 302 Found in 26ms (ActiveRecord: 7.0ms)

更新

这是comment模型:

class Comment < ActiveRecord::Base
 belongs_to :post
 attr_accessible :body, :commenter
 attr_accessor :body, :commenter
end

post控制器:

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    begin
       @post = Post.find(params[:id])

        respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @post }
        end
    rescue ActiveRecord::RecordNotFound
        redirect_to posts_path
    end
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end

最后是.erb:

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
 <%= @post.name %>
</p>

<p>
  <b>Title:</b>
  <%= @post.title %>
</p>

<p>
  <b>Content:</b>
  <%= @post.content %>
</p>

<h2>Comments</h2>
<% @post.comments.each do |comment| %>
  <p>
    <b>Commenter:</b>
    <%= comment.commenter %>
  </p>

  <p>
    <b>Comment:</b>
    <%= comment.body %>
  </p>
<% end %>

<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>

一定有一些明显的东西我在这里失踪了。

4

1 回答 1

1

啊,来了!(加了空格)

class Comment < ActiveRecord::Base
 belongs_to :post
 attr_accessible :body, :commenter

 attr_accessor :body, :commenter

end

通过添加访问器方法,我绕过了 rails 对数据成员的直接访问。我对此发表了评论,然后一切正常。我想我认为那必须在那里,因为我有这些MASS-ASSIGN错误。

感谢您的宝贵意见!

于 2012-12-17T16:56:45.370 回答