Ruby on rails table not save a additional object
问题描述:
我的用户模型中有一个has_many:active_posts表。我相当确信它设置正确。但是,当我尝试将已经存在的对象添加到表中时,它似乎不起作用。任何帮助,将不胜感激!Ruby on rails table not save a additional object
模型user.rb
def addpost(other_post)
active_posts << other_post
end
认为_feed.html.erb
<%= form_for @training_post, :html => {:class => "form-inline"}, url: confirm_training_post_path(post), method: :patch do |f| %>
<div class="form-group">
<%= f.submit "Confirm", class: "btn-primary btn-xs form-control" %>
</div>
<% end %>
控制器posts_controller.rb
def confirm
@post = Post.find_by(id: params[:id])
@user = User.find(params[:id])
@post.toggle!(:confirm)
@post.update_attribute(:propreceived, active_post_params[:propreceived])
current_user.addpost(@post)
current_user.deletepassivepost(@post)
redirect_to root_url
end
答
这听起来像你不保存用户的active_posts,而不是将其添加到集合中。您需要以某种方式保存用户的active_posts。
active_posts
假设具有user_id
和post_id
一个模式:
user.rb
def add_post(post)
self.active_posts.create(:post => post)
end
+0
谢谢!我不得不配置我的form_for实际构建另一个active_post,而不是将其添加到集合中。 –
这是其他属性? – Nithin