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.
Your model can be simpler.