Adding A Radio Button To Rails App -


i'm trying add radio button "accounts" form. however, when selecting radio button on form, doesn't seem save in show.html.erb nor in database.

this "_form.html.erb"

    <%= form_for(@account) |f| %>       <% if @account.errors.any? %>         <div id="error_explanation">            <h2><%= pluralize(@account.errors.count, "error") %> prohibited account being saved:</h2>            <ul>           <% @account.errors.full_messages.each |message| %>             <li><%= message %></li>           <% end %>           </ul>         </div>       <% end %>        <div class="form-group">         <%= f.label :first_name %><br>         <%= f.text_field :first_name, class: "form-control" %>       </div>       <div class="form-group">         <%= f.label :last_name %><br>         <%= f.text_field :last_name, class: "form-control" %>         <div class="form-group">          <%= f.label :return %><br>         <%= f.radio_button :return,  class: "form-control" %> returning client?          <div class="form-group">         <%= f.label :program %><br>         <%= f.collection_select :program_id, program.all, :id, :program, {prompt: "choose program"}, {class: "btn btn-default dropdown-toggle"} %>       </div>       <div class="form-group">         <%= f.label :address %><br>         <%= f.text_field :address, class: "form-control" %>       </div>       <div class="form-group">         <%= f.label :phone %><br>         <%= f.text_field :phone, class: "form-control" %>       </div>       <div class="actions">         <%= f.submit class: "btn btn-primary" %>       </div>     <% end %>   <!-- end snippet --> 

accounts_controller

 class accountscontroller < applicationcontroller       before_action :authenticate_user!       before_action :set_account, only: [:show, :edit, :update, :destroy]        respond_to :html        def index         @account = account.all         respond_with(@account)       end        def show         @notes = note.where(account_id: @account.id) #where note belong current account       end        def new         @account = account.new         respond_with(@account)       end        def edit       end        def create         @account = account.new(account_params)         @account.save         respond_with(@account)       end        def update         @account.update(account_params)         respond_with(@account)       end        def destroy         @account.destroy         respond_with(@account)       end        private         def set_account           @account = account.find(params[:id])         end          def account_params           params.require(:account).permit(:first_name, :last_name, :return, :program_id, :address, :phone)         end     end 

the migration file created add "return" field accounts table. added field schema.

  class addreturntoaccounts < activerecord::migration   def change     add_column :accounts, :return, :boolean, default: false   end end 

output rails server

 parameters: {"id"=>"1"}   user load (0.2ms)  select  "users".* "users"  "users"."id" = 1  order "users"."id" asc limit 1   account load (0.1ms)  select  "accounts".* "accounts"  "accounts"."id" = ? limit 1  [["id", 1]]   program load (0.1ms)  select  "programs".* "programs"  "programs"."id" = ? limit 1  [["id", 1]]   note load (0.2ms)  select "notes".* "notes"  "notes"."account_id" = 1   user load (0.1ms)  select  "users".* "users"  "users"."id" = ? limit 1  [["id", 1]]   cache (0.0ms)  select  "users".* "users"  "users"."id" = ? limit 1  [["id", 1]]   rendered accounts/show.html.erb within layouts/application (4.2ms)   rendered layouts/_bootstrap.html.erb (0.1ms)   rendered layouts/_bootstrap.html.erb (0.1ms)   rendered layouts/_navbar.html.erb (0.5ms)   rendered layouts/_footer.html.erb (0.1ms) completed 200 ok in 82ms (views: 79.2ms | activerecord: 0.7ms) 

i'm not sure value being passed correctly, guess i'm missing i'm not sure.

the radio_button form helper takes in value parameter. so, way now, helper method thinks hash ({class: "form-control"}) value. rendered radio button looks like:

<input type="radio" value="{:class=>&quot;form-control&quot;}" name="account[return]" id="account_return_classform-control"> returning client? 

pass in value persist if radio button selected:

<%= f.radio_button :return, true, class: "form-control" %> returning client? 

this give render input you'd expect:

<input class="form-control" type="radio" value="true" checked="checked" name="account[return]" id="account_return_true"> 

however, when comes boolean type, might want consider checkbox instead.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -