1

我有三个模型,用户、帖子和书籍,通过第四个 kms 关联。

  • 后模型有许多公里和书籍通过
  • 图书模型有很多公里和帖子,通过
  • km 属于 book 和 post

我已经针对上述案例尝试了本教程并且成功了。

现在,我想创建一个关联,声明一个 Post。

  • 用户 has_many kms
  • 公里属于用户

协会:

class User < ActiveRecord::Base
  include Tenantable::Schema::Model
  has_many :kms, dependent: :destroy

  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me


end

class Post < ActiveRecord::Base
  attr_accessible :name, :year, :author, :book_ids
  has_many :kms
  has_many :books, through: :kms   
end

class Book < ActiveRecord::Base
  attr_accessible :name
  has_many :kms
  has_many :posts, through: :kms
end

class Km < ActiveRecord::Base
  attr_accessible :count, :book_id, :post_id, :user_id
  belongs_to :book
  belongs_to :post
  belongs_to :user
end

这是在 PostsController 上创建的样子:

def create
  #this not working
  @post = current_user.build.kms(params[:post]) 
  @post.save
end

这里的视图表单创建看起来像:

<%= form_for @post, :url => create_post_subdomain_path(@post), :method => :post  do |f| %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :year, "Year" %><br />
    <%= f.text_field :year %>
  </div>
  <div class="field">
    <%= f.label :author, "Author" %><br />
    <%= f.text_field :author %>
  </div>
  <div class="field">
    <%= f.label "Book" %><br />
    <%= hidden_field_tag "post[book_ids][]", nil %>
     <% Book.all.each_with_index do |book, index| %>
         <%= label_tag dom_id(book) do %>
          <%= check_box_tag "post[book_ids][]", book.id, @post.book_ids.include?(book.id), id: dom_id(book) %> <%= book.nama %>
          <% end %>&nbsp;
         <%= '<br/><br/>'.html_safe if index == 3 %>
    <% end %>

  </div>
    <br/>
  <div class="actions">
    <%= f.submit ( "Save" ), :class => "btn btn-inverse btn-medium" %>&nbsp;&nbsp; or &nbsp;&nbsp;<%= link_to 'Cancel', list_book_subdomain_path, :class => "btn btn-medium btn-primary" %>
  </div>
<% end %>

current_user.id尝试km.user_iduser创建post.

但我得到了错误

Can't mass-assign protected attributes: name, year, author, book_ids

有什么建议么?

先感谢您

4

3 回答 3

1

问题可能出在 PostsController 的创建操作中。尝试以下操作:

def create
  @post = Post.create(params[:post])
  @km = current_user.kms.build(:post_id => @post.id)
  @km.save
end

您仍然需要在 Km 上设置 book_id ,但我不清楚这将如何工作,因为您的帖子说它可以has_many :books, :through => :kms,但是 Km belongs_to :book。那没有意义。你book_ids在邮报上做什么?

于 2013-04-11T12:25:07.830 回答
0

也许你想要

@post = Post.create(params[:post])
current_user.kms.create(post_id: @post.id) 

反而?

于 2013-04-11T12:21:44.333 回答
0

解决了

我找到了一个解决方案,一个像这样的解决方案: @postcreate and update all column user_idon kmwhere @post_id => @post.id,所以输出会是这样的

@post = Post.create(params[:post])
Km.where(:post_id => @post.id).update_all(:user_id => current_user.id)

输出

#<Km id: 1, post_id: 1, user_id: 1>,
#<Km id: 2, post_id: 1, user_id: 1>,
#<Km id: 3, post_id: 1, user_id: 1>,
于 2013-04-18T06:47:49.780 回答