collection_select字段嵌套形式rails 3.1

发布于 2024-12-26 11:04:32 字数 1719 浏览 2 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2025-01-02 11:04:32

您的模型可以更简单。

class User
 has_many :boards, dependent: :destroy
 accepts_nested_attributes_for :boards
end

class Board
 has_many :posts, :dependent => :destroy , :autosave => true
 belongs_to :user
 accepts_nested_attributes_for :posts
end

class Post
 belongs_to :board
end

<%= form_for(@post, :html => {:multipart => true}) do |f| %>
<%= f.label :content %><br />
<%= f.text_area :content %>
<%= f.collection_select :board_id, Board.find_by_user_id(current_user.id), :id, :name%>
<%= f.submit %>
<% end %>

Your model can be simpler.

class User
 has_many :boards, dependent: :destroy
 accepts_nested_attributes_for :boards
end

class Board
 has_many :posts, :dependent => :destroy , :autosave => true
 belongs_to :user
 accepts_nested_attributes_for :posts
end

class Post
 belongs_to :board
end

<%= form_for(@post, :html => {:multipart => true}) do |f| %>
<%= f.label :content %><br />
<%= f.text_area :content %>
<%= f.collection_select :board_id, Board.find_by_user_id(current_user.id), :id, :name%>
<%= f.submit %>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文