collection_select字段嵌套形式rails 3.1
我有 3 个模型,第一个 user.rb:
class User
has_many :boards, dependent: :destroy
has_many :posts, dependent: :destroy, :autosave => true
accepts_nested_attributes_for :boards
accepts_nested_attributes_for :posts
end
第二个模型是 board.rb
class Board
has_many :posts, :dependent => :destroy , :autosave => true
accepts_nested_attributes_for :posts
belongs_to :user
end
第三个模型是 post.rb
class Post
belongs_to :user
belongs_to :board
end
我想用 parent 创建一个新帖子board 在我的操作中,来自 posts_controllers 的新操作我有:
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
我创建了一个新板:
def create
@board = current_user.boards.new(params[:board])
respond_to do |format|
if @board.save
format.html { redirect_to @board, notice: 'Board was successfully created.' }
format.json { render json: @board, status: :created, location: @board }
else
format.html { render action: "new" }
format.json { render json: @board.errors, status: :unprocessable_entity }
end
end
end
在部分_form.thml.erb中我有这个:
<%= form_for(@post, :html => {:multipart => true}) do |f| %>
<%= f.label :content %><br />
<%= f.text_area :content %>
<%= f.collection_select :board_id, Board.all, :id, :name%>
<%= f.submit %>
<% end %>
问题是在我的选择字段中出现每个板。 我只想显示和选择属于当前用户的图板。
注意我正在使用 mongoid。
I have 3 model the first user.rb:
class User
has_many :boards, dependent: :destroy
has_many :posts, dependent: :destroy, :autosave => true
accepts_nested_attributes_for :boards
accepts_nested_attributes_for :posts
end
The second model its board.rb
class Board
has_many :posts, :dependent => :destroy , :autosave => true
accepts_nested_attributes_for :posts
belongs_to :user
end
The third model its post.rb
class Post
belongs_to :user
belongs_to :board
end
I want create a new post with a parent board in my action new from posts_controllers I have:
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
I create a new board with:
def create
@board = current_user.boards.new(params[:board])
respond_to do |format|
if @board.save
format.html { redirect_to @board, notice: 'Board was successfully created.' }
format.json { render json: @board, status: :created, location: @board }
else
format.html { render action: "new" }
format.json { render json: @board.errors, status: :unprocessable_entity }
end
end
end
In partial _form.thml.erb I have this:
<%= form_for(@post, :html => {:multipart => true}) do |f| %>
<%= f.label :content %><br />
<%= f.text_area :content %>
<%= f.collection_select :board_id, Board.all, :id, :name%>
<%= f.submit %>
<% end %>
The problem is that in my select field appear every boards. I want only show and choose the boards that belongs_to the current_user.
Note I'm using mongoid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模型可以更简单。
Your model can be simpler.