更新:解决。感谢BusyMark!点击 编辑:这是基于以下BushyMark答案修订。我认为我正确地提出的所有建议的修改,但我仍然不能得到post_type_id保存到数据库中,当用户提交的消息。

我的应用程序有一个个人资料页。在个人资料页面,该页面的所有者可以键入更新,然后点击“提交”。所述更新消息出现没有页面重新加载(即阿贾克斯)。

基本上,它看起来像你的Twitter主页。但也有一个消息的“类型”是我们的用户输入的更新时选择。消息类型是一个预填充列表。我将它的模型和装载它作为种子数据中的应用程序。

所以:有一个轮廓模型,柱模型,和一个post_type模型

profile has_many :posts结果 post belongs_to :post_type结果 post_type has_many :posts结果 post模型还具有这样的:attr_accessible :message, :post_type_id

routes.rb中看起来像这样:结果 map.resources:用户,:has_one => :profile, :has_many => :posts结果 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 %>

这是背景。这里的问题。随着我的当前设置,当用户提交他的消息,该消息被发送到职位数据库表,但POST_ID没有。

我选择标签渲染这样的:点击     <select id="post_post_type_id" name="post[post_type_id]"><option value="">Please select</option>

这是输出我在日志中得到,当我提出一个帖子:点击 处理PostsController#创建(对于IP在2009-09-03 3点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')

下面是我的架构中的有关部分,每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`
有帮助吗?

解决方案

两件事情会在这里看到这么远:

型是在导轨的保留字。它是用于单表继承。如果你改变你的列名类似“post_type”它应该帮助极大,试图解决问题随机。我通过这一个又一会儿回来,并敲打着我的大脑之前,我知道STI。

此外,你必须在你的后一个TYPE_ID。 。 。但我没有看到你的帖子:belongs_to :type。 。 。不要忘记,与外键的需求模型此声明来使用所有的ActiveRecord关联。

希望这有助于!

另一个编辑:我注意到,在您的INSERT语句,看来你有一个“类型”列?使用关系时的这也将导致问题。您是否使用了单独的“类型”的模式?你都不可能发表您的架构,如果上述任何陈述不帮?

在ActiveRecord的,一种类型的柱被保留,并且将无法正常显示(回我关于单表继承语句)。在这一点上,我强烈建议摆脱你的类型和TYPE_ID列,并给予您的文章“post_type_id”栏目。

然后,一旦你创建post_type你会想,以确保您的has_many :posts(因为它有外键)单独Post :belongs_to :post_type模型。一旦你这样做,你将能够调用post_object.post_type,它会回报你正在寻找的模型关系。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top