ruby on rails - Form options as radio buttons or regular buttons? -
i'm new rails, doing few tutorials, , don't have background in web development, apologies in advance.
i'm creating pretty simple app allows user select 1 of 3 available status updates. should store these 3 status updates string or integer? currently, postgres database stores statuses integers, though i'm curious if there's way can specify numbers 1, 2, or 3 can inserted.
second question: want display form shows 3 buttons, each button corresponding specific, set status update. want button display string inside it's human readable, status update corresponding either 1, 2, or 3. user can choose status, change status they've selected if change mind before submitting, , hit submit button once they've made mind.
should use radio buttons accomplish this? if so, how should make radio button displays text , saves integer? or should use normal button?
many thanks!
update:
thanks @bot, able create hash defining human readable values corresponding status integers so:
config/application.rb:
module myappname class application < rails::application indent_statuses = {1 => "due", 2 => "shipping", 3 => "shipped", 4 => "closed"}.freeze end end
and _form.html.erb:
<%= simple_form_for(@status) |f| %> <%= f.error_notification %> <div class="form-inputs"> <%= f.input :status , :as => :radio_buttons ,:collection => myappname::application::indent_statuses.keys, :label_method => lambda { |k| myappname::application::indent_statuses[k]}, :item_wrapper_class => 'inline', :include_blank => false %> </div> <div class="form-actions"> <%= f.button :submit %> </div> <% end %>
however in index.html.erb, status displays number rather human readable version:
<h1>listing statuses</h1> <table> <tr> <th>status</th> <th></th> <th></th> <th></th> </tr> <% @statuses.each |status| %> <tr> <td><%= status.status %></td> <td><%= link_to 'show', status %></td> <td><%= link_to 'edit', edit_status_path(status) %></td> <td><%= link_to 'destroy', status, method: :delete, data: { confirm: 'are sure?' } %></td> </tr> <% end %> </table> <br /> <%= link_to 'new status', new_status_path %>
how status.status
show human readable value of hash created?
i used scaffolding build statuses, status controller you'd expect.
to have key value pair based fields in rails app, need first define hash containing pairs. if it's going constant across application can use config/application.rb file define it.
module myappname class application < rails::application indent_statuses = {1 => "due", 2 => "shipping", 3 => "shipped", 4 => "closed"}.freeze end end
then in form can if using simple_form.
<%= f.input :status , :as => :radio_buttons ,:collection => myappname::application::indent_statuses.keys, :label_method => lambda { |k| myappname::application::indent_statuses[k]}, :item_wrapper_class => 'inline', :include_blank => false %>
and don't forget restart server after adding in application.rb file. hope helps.
update
now display status in show pages need value corresponding status key hash.
myappname::application::indent_statuses[indent.status.to_i]
you can use directly in view.
<td><%= myappname::application::indent_statuses[status.status.to_i] %></td>
or can define method in model.
class status # # def status_label myappname::application::indent_statuses[self.status.to_i] end end
and in view use this.
<td><%= status.status_label %></td>
Comments
Post a Comment