Rails 一个页面显示的疑惑,很希望 @poshboytl 能回答一下

ane · 2013年09月26日 · 最后由 u1378130755 回复于 2013年09月26日 · 2393 次阅读
first

hello world1

Comments

Commented about 1 hour 
沙发

Commented 44 minutes 
Commented 44 minutes 
Commented 44 minutes 
Commented 41 minutes 
Commented 34 minutes 
Commented 3 minutes 
Commented 1 minute 
Commented less than a minute 
[#<Thecomment id: 1, post_id: 1, content: "沙发", created_at: "2013-09-26 07:10:02", updated_at: "2013-09-26 07:10:02">, #<Thecomment id: 4, post_id: 1, content: nil, created_at: "2013-09-26 07:28:21", updated_at: "2013-09-26 07:28:21">, #<Thecomment id: 5, post_id: 1, content: nil, created_at: "2013-09-26 07:28:23", updated_at: "2013-09-26 07:28:23">, #<Thecomment id: 6, post_id: 1, content: nil, created_at: "2013-09-26 07:28:33", updated_at: "2013-09-26 07:28:33">, #<Thecomment id: 7, post_id: 1, content: nil, created_at: "2013-09-26 07:31:06", updated_at: "2013-09-26 07:31:06">, #<Thecomment id: 8, post_id: 1, content: nil, created_at: "2013-09-26 07:38:24", updated_at: "2013-09-26 07:38:24">, #<Thecomment id: 9, post_id: 1, content: nil, created_at: "2013-09-26 08:09:10", updated_at: "2013-09-26 08:09:10">, #<Thecomment id: 10, post_id: 1, content: nil, created_at: "2013-09-26 08:10:46", updated_at: "2013-09-26 08:10:46">, #<Thecomment id: 11, post_id: 1, content: nil, created_at: "2013-09-26 08:12:07", updated_at: "2013-09-26 08:12:07">

如图,为什么会显示一些数据库字段的信息了?

编写环境 ruby 2.0.rails 4.0.

另外,错误提示为:

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"aZGt593fuXOEd0pe5ocmk5dqxTb86k2MUwqUU5o45MM=",
 "thecomment"=>{"content"=>"e e"},
 "commit"=>"Add thecomment",
 "post_id"=>"1"}

class ThecommentsController < ApplicationController
  def create
   @user1 = Post.find(params[:post_id])
   @user2 = @post.thecomments.new(params[:thecomment])
   redirect_to @post if @thecomment.save
  end
end

提交 content,全为 nil,证明页面字段没对应上后台啊。 其中 thecomment 中的 content 提交的值为 e e.但是 ThecommentsController 代码中,参数用的 thecomment,是不是有问题,感觉应该是 thecomment.content 之类的才对啊。

很希望@poshboytl (也不知道可不可以@)能回答一下,因为我是做的你的第一个视频“Rails Tutorial”

我把代码贴全了,还是希望有人可以解答一下疑惑 post.rb

class Post < ActiveRecord::Base
    validates :title, :presence=>true,:uniqueness => true
    validates :content,:presence=>true
    has_many :thecomments
end

thecomment.rb

class Thecomment < ActiveRecord::Base
    belongs_to :post
end

20130926034433_create_posts.rb

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.text :content

      t.timestamps
    end
  end
end

20130926070204_create_thecomments.rb

class CreateThecomments < ActiveRecord::Migration
  def change
    create_table :thecomments do |t|
      t.integer :post_id
      t.text :content

      t.timestamps
    end
  end
end

thecomments_controller.rb

class ThecommentsController < ApplicationController
  def create
   @post = Post.find(params[:post_id])
   @thecomment = @post.thecomments.new(params[:thecomment])
   redirect_to @post if @thecomment.save
  end
end

_post.html.erb

<div id="post">
    <h2> <%= link_to_unless_current post.title,post %></h2> 
    <%= simple_format post.content %> 
  </div> 

show.html.erb

<%= render :partial => @post %>
<h2>Thecomments</h2>
<%= @post.thecomments.each do |thecomment|%>
<div id="comment">
    <p>
       <strong>Commented <%= time_ago_in_words(thecomment.created_at)%></strong>
       <br/> 
       <%= thecomment.content %>
    </p>
 </div> 
 <% end %>  
 <%= form_for [@post, Thecomment.new] do |f| %>
<p>
    <%= f.label :content,"New Thecomment" %>
    <br/>
    <%= f.text_area :content %>
</p>
<p><%= f.submit "Add thecomment" %></p>
 <% end %> 
 <br/>   
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>

猜测是页面上循环时加了 = 比如 <%= comments.each do |comment| %> 这里最前面那个=去掉

#1 楼 @kenshin54 没错,那个数据库的信息确实如此,很想问为什么会这个样子。更加想知道为什么提交 content 都为 null

人家视频里的代码是

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(params[:comment])
    redirect_to @post if @comment.save
  end
end

楼主比对下就能发现了 你用的@user1@user2 然后又用了@thecomment做的 save 明显是驴唇对不上马嘴... 还有 rails 是木有依赖注入的 controller 里的变量是需要自己命名 楼主把 spring 忘掉吧

#3 楼 @zj0713001 不是的,

那个 user1 和 user2 是我发布上来变成那样的,因为我选择成 html/erb 的模式 代码里依旧是

class ThecommentsController < ApplicationController
  def create
   @post = Post.find(params[:post_id])
   @thecomment = @post.thecomments.new(params[:thecomment])
   redirect_to @post if @thecomment.save
  end
end

我只是把他原来用的 comment 换成 thecomment,所以应该是没有问题的啊

需要 登录 后方可回复, 如果你还没有账号请 注册新账号