In has_many or habtm association we sometime need to store array of id's.
So, this is how we do it.
table : Projects, :Categories
Model: Project, Category
html form
= f.select :category_ids, Category.all.collect{|p| [p.name, p.id]},{:include_blank => false, :prompt=>"select catgory" }, :multiple=>true
And in the controller maintain your normal flow.
def create
if params
@project = Project.create( params[:project])
#@project.categories << params[:project][:category_ids]
if @project.save
flash[:success] = "Project created successfully"
redirect_to :action=>"list"
return
else
@project.screenshot = nil
params[:project][:screenshot] = nil
flash[:error] = @project.errors
end
end
render :action=>"new"
end
So, this is how we do it.
table : Projects, :Categories
Model: Project, Category
html form
= f.select :category_ids, Category.all.collect{|p| [p.name, p.id]},{:include_blank => false, :prompt=>"select catgory" }, :multiple=>true
And in the controller maintain your normal flow.
def create
if params
@project = Project.create( params[:project])
#@project.categories << params[:project][:category_ids]
if @project.save
flash[:success] = "Project created successfully"
redirect_to :action=>"list"
return
else
@project.screenshot = nil
params[:project][:screenshot] = nil
flash[:error] = @project.errors
end
end
render :action=>"new"
end
No comments:
Post a Comment