Collection_select问题
更新:已解决。谢谢 BusyMark!
编辑:这是根据 BushyMark 的以下答案进行修订的。我认为我正确地进行了所有建议的更改,但当用户提交消息时,我仍然无法将 post_type_id 保存到数据库中。
我的应用程序有一个个人资料页面。在个人资料页面上,页面所有者可以输入更新并点击“提交”。更新消息无需重新加载页面(即 Ajax)即可显示。
它基本上看起来就像你的 Twitter 主页。但是,我们的用户在键入更新时还可以选择一条消息“类型”。消息类型是预先填充的列表。我已将其作为模型并将其作为种子数据加载到应用程序中。
所以:有一个 profile 模型、一个 post 模型和一个 post_type 模型。
个人资料 has_many :posts
帖子所属:post_type
post_type has_many :posts
帖子模型也有这样的:attr_accessible :message, :post_type_id
Routes.rb 看起来像这样:
map.resources :users, :has_one =>; :个人资料, :has_many => :帖子
map.resources :post_types
帖子控制器具有用于创建帖子的方法:
def create
@post = current_user.profile.posts.build(:message => params[:message], :post_type_id => params[:post_type_id])
if @post.save
redirect_to user_profile_path
else
flash[:notice] = "Message failed to save."
redirect_to user_profile_path
end
end
发生这一切的个人资料页面的“显示”视图如下所示:
<% form_tag(:controller => "posts", :action => "create") do %>
<%= label_tag(:message, "Update your people") %><br />
<%= text_area_tag(:message, nil, :size => "27x3") %>
<div class="updateSubmit"><%= submit_tag("Update") %></div>
<%= label_tag(:type_id, "Optional: Update type", :class => 'typeLabel') %>
<%= collection_select(:post, :post_type_id, PostType.all, :id, :name, {:prompt => true}) %>
<% end %>
<% @profile.profile.posts.each do |c| %>
<%=h c.message %></div>
<p><%=h c.post_type %></p>
<p>Posted <%=h time_ago_in_words(c.created_at) %> ago</p>
<% end %>
这就是背景。问题就在这里。在我当前的设置中,当用户提交消息时,消息将被发送到 posts 数据库表,但 post_id 不会。
我的选择标签呈现如下:
这是我在日志中得到的输出我提交一个帖子:
处理 PostsController#create (for IP at 2009-09-03 03:03:08) [POST]
Parameters: {"message"=>"This is another test.", "commit"=>"Update ", "action"=>"创建", "authenticity_token"=>"已编辑", "post"=>{"post_type_id"=>"3"}, "controller"=>"posts", "user_id"=>"1"}
用户加载 (0.3ms) SELECT * FROM "users" WHERE ("users"."id" = 1)
配置文件加载(0.7ms) SELECT * FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1
Post Create (0.5ms) INSERT INTO "posts" ("message", "updated_at", " post_type_id", "profile_id", "created_at") VALUES('Hello', '2009-09-03 20:40:24', NULL, 1, '2009-09-03 20:40:24')
这是我的架构的相关部分,根据 BushyMark 的评论进行了更新:
`create_table "post_types", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end`
`create_table "posts", :force => true do |t|
t.text "message"
t.integer "profile_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "post_type_id"
end`
UPDATE: Solved. Thanks BusyMark!
EDIT: This is revised based on the answer below from BushyMark. I think I made all the suggested changes correctly, but I still can't get the post_type_id to save to the database when a user submits a message.
My app has a profile page. On the profile page, the owner of the page can type an update and hit "submit." The update messages appear without the page reloading (i.e. Ajax).
It basically looks like your Twitter home page. BUT, there's also a message "type" that our users select when typing an update. The message type is a pre-populated list. I've made it a model and load it as seed data in the app.
So: there is a profile model, a post model, and a post_type model.
profile has_many :posts
post belongs_to :post_type
post_type has_many :posts
The post model also has this: attr_accessible :message, :post_type_id
Routes.rb looks like this:
map.resources :users, :has_one => :profile, :has_many => :posts
map.resources :post_types
The posts controller has this method for creating the posts:
def create
@post = current_user.profile.posts.build(:message => params[:message], :post_type_id => params[:post_type_id])
if @post.save
redirect_to user_profile_path
else
flash[:notice] = "Message failed to save."
redirect_to user_profile_path
end
end
The "show" view for the profile page where this all occurs looks like this:
<% form_tag(:controller => "posts", :action => "create") do %>
<%= label_tag(:message, "Update your people") %><br />
<%= text_area_tag(:message, nil, :size => "27x3") %>
<div class="updateSubmit"><%= submit_tag("Update") %></div>
<%= label_tag(:type_id, "Optional: Update type", :class => 'typeLabel') %>
<%= collection_select(:post, :post_type_id, PostType.all, :id, :name, {:prompt => true}) %>
<% end %>
<% @profile.profile.posts.each do |c| %>
<%=h c.message %></div>
<p><%=h c.post_type %></p>
<p>Posted <%=h time_ago_in_words(c.created_at) %> ago</p>
<% end %>
That's the background. Here's the problem. With my current setup, when the user submits his message, the message gets sent to the posts database table, BUT the post_id does not.
My select tag is rendering like this:<select id="post_post_type_id" name="post[post_type_id]"><option value="">Please select</option>
This is the output I get in the log when I submit a post:
Processing PostsController#create (for IP at 2009-09-03 03:03:08) [POST]
Parameters: {"message"=>"This is another test.", "commit"=>"Update", "action"=>"create", "authenticity_token"=>"redacted", "post"=>{"post_type_id"=>"3"}, "controller"=>"posts", "user_id"=>"1"}
User Load (0.3ms) SELECT * FROM "users" WHERE ("users"."id" = 1)
Profile Load (0.7ms) SELECT * FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1
Post Create (0.5ms) INSERT INTO "posts" ("message", "updated_at", "post_type_id", "profile_id", "created_at") VALUES('Hello', '2009-09-03 20:40:24', NULL, 1, '2009-09-03 20:40:24')
Here's the relevant parts of my schema, updated per BushyMark's comments:
`create_table "post_types", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end`
`create_table "posts", :force => true do |t|
t.text "message"
t.integer "profile_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "post_type_id"
end`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止,我看到这里发生了一些事情:
type 是 Rails 中的保留字。它用于单表继承。如果您将列名称更改为“post_type”之类的名称,它应该对解决随机问题有很大帮助。不久前我经历过这件事,在我知道性传播感染之前我已经绞尽脑汁了。
另外,您的帖子中有一个 type_id 。 。 。但我没有看到你的帖子
:belongs_to :type
。 。 。不要忘记具有外键的模型需要此声明才能使用所有 ActiveRecord 关联。希望这有帮助!
另一个编辑:我注意到在你的插入语句中,你似乎有一个“类型”列?这也会在使用关系时引起问题。您是否使用单独的“类型”模型?如果上述任何陈述都没有帮助,您可以发布您的架构吗?
在ActiveRecord中,类型列被保留并且不会正确显示(回到我关于单表继承的陈述)。此时,我强烈建议删除您的 type 和 type_id 列,并为您的帖子提供一个“
post_type_id
”列。然后,一旦您创建了一个
has_many :posts
的单独post_type
模型,您将需要确保您的Post :belongs_to :post_type
(因为它具有外键)。完成此操作后,您将能够调用post_object.post_type
并且它将返回您正在查找的模型关系。Couple things going on here I see so far:
type is a reserved word in Rails. It is used for single table inheritance. If you change your column name to something like "post_type" it should help immensely in trying to solve random issues. I went through this a while back and was beating my brains out before I knew about STI.
Also, you have a type_id in your post . . . but I didn't see that your post
:belongs_to :type
. . . don't forget that a model with the foreign key needs this declaration to use all the ActiveRecord associations.Hope this helps!
Another Edit: I noticed that in your insert statement, it appears you have a "type" column? This will also cause issues when using relationships. Are you using a separate 'type' model? Can you possibly post your schema if any of the above statements don't help?
In ActiveRecord, a type column is reserved and will not display properly(back to my statement about Single Table Inheritance). At this point I strongly recommend getting rid of your type and type_id columns, and giving your Post a "
post_type_id
" column.Then once you create a separate
post_type
model thathas_many :posts
you will want to make sure yourPost :belongs_to :post_type
(because it has the foreign key). Once you do that, you will be able to callpost_object.post_type
and it will return the model relationship you are looking for.