SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Sunday, April 20, 2008

Generating multiple checkbox in ruby on rails

checkbox loop in ruby on rails


1.app/views/users/_form.html


    <% City.find(:all).each do |g| %>

  • <%= check_box_tag 'city',g.id,checked=false,{:name => "city[city_id][]", :id => "city_id_#{g.id}"}%>
    <%= g.name.capitalize %>

  • <% 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

1 comment :