【问题标题】:Collection_select problemCollection_select 问题
【发布时间】:2009-09-03 07:08:28
【问题描述】:

更新:已解决。感谢 BusyMark!
编辑:这是根据 BushyMark 的以下回答进行修改的。我认为我正确地进行了所有建议的更改,但是当用户提交消息时,我仍然无法将 post_type_id 保存到数据库中。

我的应用有个人资料页面。在个人资料页面上,页面所有者可以键入更新并点击“提交”。无需重新加载页面(即 Ajax)即可显示更新消息。

它基本上看起来像您的 Twitter 主页。但是,我们的用户在输入更新时也会选择一个消息“类型”。消息类型是一个预先填充的列表。我已将其作为模型并将其作为种子数据加载到应用中。

所以:有一个 profile 模型、一个 post 模型和一个 post_type 模型。

profile has_many :posts
post belongs_to :post_type
post_type has_many :posts
帖子模型也有这个:attr_accessible :message, :post_type_id

Routes.rb 看起来像这样:
地图资源:用户,:has_one => :profile, :has_many => :posts
map.resources :post_types

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

这一切发生的个人资料页面的“显示”视图如下所示:

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

这就是背景。这就是问题所在。使用我当前的设置,当用户提交他的消息时,消息会发送到帖子数据库表,但 post_id 不会。

我的选择标签呈现如下:
&lt;select id="post_post_type_id" name="post[post_type_id]"&gt;&lt;option value=""&gt;Please select&lt;/option&gt;

这是我提交帖子时在日志中得到的输出:
处理 PostsController#create (for IP at 2009-09-03 03:03:08) [POST]

Parameters: {"message"=&gt;"This is another test.", "commit"=&gt;"Update", "action"=&gt;"create", "authenticity_token"=&gt;"redacted", "post"=&gt;{"post_type_id"=&gt;"3"}, "controller"=&gt;"posts", "user_id"=&gt;"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')

这是我的架构的相关部分,根据 BushyMark 的 cmets 进行了更新:

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

【问题讨论】:

    标签: ruby-on-rails models helpers controllers


    【解决方案1】:

    到目前为止,我看到这里发生了一些事情:

    type 是 Rails 中的保留字。它用于单表继承。如果您将列名更改为“post_type”之类的名称,它应该会极大地帮助您解决随机问题。在我了解 STI 之前,我曾经经历过这件事,并且正在绞尽脑汁。

    此外,您的帖子中有一个 type_id 。 . .但我没有看到你的帖子 :belongs_to :type 。 . .不要忘记具有外键的模型需要此声明才能使用所有 ActiveRecord 关联。

    希望这会有所帮助!

    另一个编辑:我注意到在您的插入语句中,您似乎有一个“类型”列?这也会在使用关系时引起问题。您是否使用单独的“类型”模型?如果上述任何陈述都没有帮助,您可以发布您的架构吗?

    在 ActiveRecord 中,类型列被保留,无法正确显示(回到我关于单表继承的陈述)。在这一点上,我强烈建议去掉你的 type 和 type_id 列,并给你的 Post 一个“post_type_id”列。

    然后,一旦您创建了一个单独的post_type 模型has_many :posts,您将需要确保您的Post :belongs_to :post_type(因为它具有外键)。一旦你这样做了,你就可以打电话给post_object.post_type,它会返回你正在寻找的模型关系。

    【讨论】:

    • 我做了所有这些更改,但是当用户提交时,我仍然无法将 post_type_id 保存在数据库中。我将使用新设置修改我的问题。
    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多