2017-06-02 88 views
0

我的用戶模型中有一個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 
+0

這是其他屬性? – Nithin

回答

0

這聽起來像你不保存用戶的active_posts,而不是將其添加到集合中。您需要以某種方式保存用戶的active_posts。

active_posts假設具有user_idpost_id一個模式:

user.rb 

def add_post(post) 
    self.active_posts.create(:post => post) 
end 
+0

謝謝!我不得不配置我的form_for實際構建另一個active_post,而不是將其添加到集合中。 –