jeudi 18 juin 2015

Form value not submitted through checkbox Rails 4

I have a number of check-boxes based on roles of each user with a many-to-may relationship. But when I try to provide these roles, it does not seem to get saved, although the form returns no error. When I try it out in console, User.find(1).role_ids = 3 it gets saved and returns check-box ticked when viewed on webpage. I am new to Rails and I need a little help to get past this.

My .html.erb

<%= form_for(:user, url: {action: 'update', id: @user.id}) do |f| %>
<tr>
    <% for role in Role.all %>
    <td><%= check_box "user[role_ids][]", role.id, @user.roles.include?(role) %></td><td></td><td></td><td></td><td></td><td></td><td></td>
    <% end %>
</tr>

My controller.rb

    def edit 
        @user = User.find(params[:id])
      end
    def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:notice] = "Role assigned successfully" 
      redirect_to(:action => 'show', :id => @user.id)
    else
      render('edit')
    end
  end




Aucun commentaire:

Enregistrer un commentaire