Friday, 15 March 2013

jquery - Assigning model instance attributes based on a checkbox value -



jquery - Assigning model instance attributes based on a checkbox value -

i have user model has stage_name , real_name string attributes. i'd in _form.hmlt.erb have checkbox next real name input box asks, real name stage name?. if checked i'd assign real name stage name , disable stage name input box withreal_name` real name value. far have:

_form.html.erb

<%= form_for @user |f| %> <div> <%= f.text_field :stage_name %> <%= f.text_field :stage_name %> </div> <div> <%= f.text_field :real_name %> <%= f.text_field :real_name %> # not sure how bind model code checkbox <%= f.checkbox %> <%= f.label 'real name stage name?' %> </div> <div> <%= f.submit %> </div> <% end %> <script> $(function() { $("input[type='checkbox']").click(function() { var $checkbox = $(this), $secondform = $("input[type='text']").eq(2); if ($checkbox.is(":checked")) { $secondform.attr("disabled", true).val("<%= @user.real_name %>"); } else { $secondform.attr("disabled", false).val(""); } }); )}; </script>

all need assign attribute accessor in model.

attr_accessor :real_name_is_stage_name

this create non-persistent attribute in model can check in 1 of callbacks, if value 1 set real_name = stage_name example:

before_save if real_name_is_stage_name == "1" real_name = stage_name end end

then set checkbox interact changes made can send value of checkbox model follows:

<%= f.check_box :real_name_is_stage_name %>

if so, in javascript code need disable stage name text_field when check_box checked , rails rest of job

model.rb

class model < activerecord::base attr_accessor :name_check ## validation ## callbacks before_validation if self.name_check == "1" , self.stage_name.present? , !self.real_name.present? self.real_name = self.stage_name end end end

_form.html.erb

<%= form_for @user |f| %> <%= render 'shared/error_messages', object: @user %> <div> <%= f.label :stage_name %><br> <%= f.text_field :stage_name %> </div> <div> <%= f.label :real_name %><br> <%= f.text_field :real_name %> <div> <%= f.label :name_check, 'real name stage name?' %> <%= f.check_box :name_check, value: f.obj.real_name.eql?(f.obj.stage_name)? "1" : "0" if f.obj.real_name.present? , f.obj.stage_name.present? %> %> </div> </div> <div> <%= f.label :origin %><br> <%= f.text_field :origin %> </div> <%= f.submit %> <% end %> <%= render 'scripts' %>

_scripts.html.erb

<script> $(function() { var $namecheck = $('#artist_name_check'), $stagename = $('#artist_stage_name'), $realname = $('#artist_real_name'), $originalval = $stagename.val(); // checks , mirrors artist's real name , stage name if ($namecheck.is(':checked')) { $stagename.attr("readonly", "readonly"); } $namecheck.click(function() { if ($namecheck.is(":checked")) { $stagename.attr("readonly", "readonly").val($realname.val()); } else { $stagename.attr("readonly", false).val($originalval).focus(); } }); }); </script>

jquery ruby-on-rails forms checkbox input

No comments:

Post a Comment