checkbox loop in ruby on rails
1.app/views/users/_form.html
<%= check_box_tag 'city',g.id,checked=false,{:name => "city[city_id][]", :id => "city_id_#{g.id}"}%>
<%= g.name.capitalize %>
<% City.find(:all).each do |g| %>
<% end %>
2.app/controllers/users
def create
if params[:article_logicalarticle][:city_id]
params[:article_logicalarticle][:city_id].each do |city_id|
@article_logicalarticle=ArticleLogicalarticle.new(params[:article_logicalarticle])
@article_logicalarticle.city_id=city_id
@article_logicalarticle.save
end
end
Finally the selected values from checkbox will be added in database.
Note:I didnt use associations for this multiple checkbox
is the :article_logicalarticle your join table?
ReplyDelete