0

私の質問はこの前の質問と多少似ていますが、私の場合、私はさらに初心者であり、私のサーバーはRuby-on-Rails入門ガイドの最初のページで壊れています。

基本的に、ガイドはコメントフォームを使用して簡単なブログ投稿アプリを作成する方法を説明します。ガイドに従って、ブログ投稿を作成すると、コメントフォームが表示されます。コメントの「show」領域もレンダリングされ、正しい数のコメントが表示されますが、db自体に格納される値は、「commenter」フィールドと「body」フィールドの両方で「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

アクセサメソッドを追加することで、データメンバーへのレールの直接アクセスをバイパスしていました。私はそれをコメントアウトし、それからすべてがうまくいきました。私はそれらのMASS-ASSIGNエラーがあったので、それはそこになければならないと思ったと思います。

貴重なコメントありがとうございます!

于 2012-12-17T16:56:45.370 に答える