winston
winston

Reputation: 3100

Set a value on form submit if checkbox is checked in Rails?

I have a form that creates a Post. A Post has a non-required field called school_id. On the Post form (to create a new Post) I have a checkbox. If that checkbox is checked, I want to set :school_id to equal the school_id that's also set to current_user (object created by Devise). How can I set the Post.school_id to equal current_user.school_id if the checkbox is checked?

The checkbox I have on the form is passing a :school_id of 1 and never changes. Is this because checkboxes can only accept boolean values of either 1 or 0? Here's what I have on the form so far:

<%= simple_form_for @post do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
    <%= f.text_area :content, required: true %>

  <% if @school %>
     <%= f.label :school_id, "Set school",:class => "checkbox inline"  %>
     <%= f.check_box :school_id, :value => current_user.school.id %>
  <% end %>

<%= f.submit "Submit Post", class: 'btn btn-primary' %>
<% end %>

EDIT

post controller

def create
  @school = current_user.school
  @post = current_user.posts.build(params[:post])
  @post.school_id = current_user.school_id if @school && @post.use_school.present?     
  end

controller with the post form

def index
    @post = current_user.posts.build  
    @school = current_user.school
    @post.school_id = current_user.school_id if @school && @post.use_school.present?
     respond_to do |format|
      format.html # index.html.erb
      format.json
      format.js 
    end
    end

Upvotes: 2

Views: 7383

Answers (1)

Julien Vignolles
Julien Vignolles

Reputation: 379

Your problem comes from Simpleform! Simpleform forces checkbox input to be a boolean. You'll have the same problem with other syntaxes:

<%= f.input :school_id, :as => :boolean, :input_html => { :value => current_user.school.id } %>

To go deeper:

<%= f.check_box :school_id, :value => current_user.school.id %>

will generate something like this:

<input type="hidden" name="post[school_id]" value="0">
<input type="checkbox" name="post[school_id]" value="1">

Note: Simpleform automatically adds the first line (a good practice) to be sure that a value (0) is sent on submit when the input is unchecked. Otherwise, you may have issues on model update.

Your field is not a boolean, you shouldn't use a checkbox. Moreover, an user can edit the value of the checkbox (firebug & co) and that may lead to inconsistent datas or hack. So, with a checkbox you should check the school_id is correct in your controller.

I suggest this workarround:

app/views/posts/new.rb:

<% if @school %>
    <%= f.label :use_school, "Set school",:class => "checkbox inline"  %>
    <%= f.input :use_school, :as => :boolean %>
<% end %>

app/models/post.rb:

attr_accessor :use_school

app/controllers/posts_controller.rb:

@post = Post.new(params[:post])
@post.school_id = current_user.school_id if @school && @post.use_school.present?

Note: the controller part may be done directly in your model with a :before_save.

Upvotes: 2

Related Questions