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 %>