mardi 10 mai 2016

Unchangable checkbox

I've been struggling with this issue for 2 days now and I think I'm slowly starting to lose my mind. I'm trying to update boolean 'schedule_display' for 'profiles' table in my nested form. Everything except this checkbox works just fine. At the present state html looks like this:

<%= nested_form_for @profile, html: { multipart: true } do |f| %>
  <span class="picture">
    <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
  </span>
  <%= f.label :description %>
  <%= f.text_field :description %>
  <%= f.label :schedule_display %>
  <%= f.check_box :schedule_display, {}, "true", "false" %>
  <%= f.fields_for :buttons %>
  <%= f.link_to_add "Add a button", :buttons %>  
  <%= f.submit "Save changes" %>
<% end %>

Parameters after submit look fine I think:

profile"=>{"description"=>"Dolor et exercitationem.", "schedule_display"=>"true", ... 

Schedule_display is also in permited params in the right place:

params.require(:profile).permit(:id, :description, :picture,
                                 :schedule_display, buttons_attributes: [

Corresponding part of Profile model looks like this:

class Profile < ActiveRecord::Base
  belongs_to :user
  has_many   :buttons,  :dependent => :destroy
  accepts_nested_attributes_for :buttons, 
    reject_if: proc { |attributes| attributes['user_website_url'].blank? }, 
    :allow_destroy => true
  mount_uploader :picture, PictureUploader
  attr_accessor :schedule_display
  validates :description, presence: true, length: { maximum: 500 }

and update method is simply:

def update
  @user = User.find(params[:id])
  @profile = @user.profile
  if @profile.update_attributes(profile_params)
    flash[:success] = "Profile updated"
    redirect_to @user
  else
    render 'edit'
  end
end

I've tried doing something like this:

if @profile.update_attributes(profile_params)
  params[:profile][:schedule_display] == '1' ? 
    profile.turn_schedule_on : profile.turn_schedule_off

with these turn_schedule_ functions and simple update_attribute(), but it didn't work either.

Why isn't it working?




Aucun commentaire:

Enregistrer un commentaire