form select is not remembering value - ruby-on-rails

Select helper is not remembering its database value. But Text field is remembering. Any ideas? Thanks.
<%= form_for([:admin, #product], :remote => true ) do |f| %>
<%= f.text_field :name %>
<%= f.select(:search_status, ["0","1","2"]) %>
<% end %>

It's likely that search_status is an integer and not matching the strings in your array. Does it work if you use [0,1,2] instead?

Related

Rails form fields empty after submitting form

This is my form:
<%= form_tag("/adverts", :method => "get") do %>
Order by:
<%= select_tag :order_by, options_for_select([['Ascending', 'ASC'], ['Descending', 'DESC']])%>
<%= text_field_tag :text%>
<%= submit_tag 'Change' %>
<% end %>
In my Adverts controller, index method, for now I am not doing anything and I can see that it is getting correct values from form,
=>but when page reloads after submission, fields values are empty but I want them to retain values.
So if I enter some text in text field, that text will still be there after submitting form .
Like this:
<%= select_tag :order_by, options_for_select([['Ascending', 'ASC'], ['Descending', 'DESC']], params[:order_by]) %>
and:
<%= text_field_tag :text, params[:text] %>
See the API for options_for_select and text_field_tag.
You need to create the form for an object if you want it to automatically get the objects values on reload.
<%= form_for #object do |form| %>
<%= form.text_field :name %> <!-- automatically gets re-populated with the value of #object on postback -->
<%= form.submit %>
<% end %>
If you really want to use form tags instead of a builder then you need to set the values manually after postback
<%= text_field_tag :text, some_string_value %>

make boolean form field hidden

I have a boolean to make comments public or private.
The boolean is a column in submissions and I have it working in a clumsy way right now and would like to eliminate the checkbox from the form, replacing with a hidden field, so that all the user sees is the submit button, conditional based on the boolean state:
submissions#show:
<% if #submission.comment_show == true %>
<%= render "hide_comment_form" %>
<%= render "comments/comment" %>
<% else %>
<%= render "show_comment_form" %>
</div>
<% end %>
_show_comment_form
<%= simple_form_for [#contest, #submission] do |f| %>
<div>
<%= f.input :comment_show, label: false %>
<%= hidden_field_tag :contest_id, #contest.id %>
<%= f.submit "Make Comments Public", :class => 'btn btn-mini' %>
</div>
<% end %>
_hide_comment_form
<%= simple_form_for [#contest, #submission] do |f| %>
<div class ="">
<%= f.input :comment_show, label: false %>
<%= hidden_field_tag :contest_id, #contest.id %>
<%= f.submit "Make Comments Private", :class => 'btn btn-mini' %>
</div>
<% end %>
I have tried hidden_field_tag, but haven't had any luck getting it to work.
Also, I've seen some fancier methods and routing to accomplish the same thing:
http://buckybits.blogspot.com/2011/09/simple-ajax-property-toggle-in-rails-30.html
But I would prefer to use a hidden field and conditionals to keep it simple.
Is it possible to use a hidden field to set a boolean in a form or do I have to go with a custom method and routing?
See the answer to this SO question: rails simple_form - hidden field - create?
Since you are using simple form, you can do something like this:
f.input :contest_id, :as => :hidden, :input_html => { :value => #contest.id }

Rails loop through data in a form field

I would like to loop through data in my database inside my form. What I would like to do with the data is put it in labels and textboxes. How could I do this in rails? Would I just use a .each block to loop through it inside the form? The reason I have it in my database is because my client would like to be able to add the form field data himself.
For example here is something I would like to do:
<%= form_for :order do |f| %>
#fields.each do |field|
<%= f.label field.name %>
<%= f.text_field field.name %>
<% end %>
<%= f.submit %>
<% end %>
What the best way to accomplish something like this?
Please don't answer with a railscast :)
Thanks in advance
Yes, that will work, though you missed an end script tag on line two:
<%= form_for :order do |f| %>
<% #fields.each do |field| %>
<%= f.label field.name %>
<%= f.text_field field.name %>
<% end %>
<%= f.submit %>
<% end %>
If you need something more complex than just a label/text field pair - then you can use a partial-template and use the collection keyword:
<!-- in 'order.html.erb' -->
<%= form_for :order do |f| %>
<!-- note: each 'field' is auto-populated from the collection/partial-name, but you need to pass the form in as a local -->
<%= render :partial => 'field', :collection => #fields, :locals => {:f => f} %>
<%= f.submit %>
<% end %>
and
<!-- in '_field.html.erb' -->
<%= f.label field.name %>
<%= f.text_field field.name %>
<!-- and whatever else you want to do... -->
more on partial rendering here: http://api.rubyonrails.org/classes/ActionView/Partials.html

Rails checkboxes and serialized data

I'm trying to figure out whats the best way to get checkboxes to properly show their current state. This is what I have in my form
<%= form_for #user, :url => user_notification_preferences_path(#user), :method => :put do |f| %>
<%= f.fields_for :notification_preferences, #user.notification_preferences do |p| %>
<%= p.check_box :notify_on_friend_post %>
<%= p.check_box :notify_on_friend_post %>
<%= p.check_box :notify_on_friend_request %>
<%= p.check_box :notify_on_friend_comment %>
<% end %>
<%= f.submit %>
<% end %>
notification_preferences is a serialized hash on my user model
class User < ActiveRecord::Base
serialize :notification_preferences, Hash
My issue that is no matter what I try, I can not get the check boxes to reflect the existing state of the hash values. IE, if the hash already contains :notify_on_friend_post => 1, then the check box for that value should be checked.
The form posts the data fine, and I'm able to update my model as well.
Update
using check_box_tag I can get this to work
<%= p.hidden_field :notify_on_friend_post, :value => "0" %>
<%= check_box_tag "user[notification_preferences][notify_on_friend_post]", "1", #user.notification_preferences[:notify_on_friend_post] == "1" ? true : false %>
ugly but working, still hoping I'm missing something very obvious
I ran into this problem and solved it in a simpler way.
<%= form_for #user do |f| %>
<%= f.fields_for :notifications, #user.notifications do |n| %>
<%= n.check_box :new_task, checked: #user.notifications[:new_task] == "1" %>
<% end %>
<%= f.submit %>
<% end %>
In this way you let the magic of check_box to the work and don't need to have a hidden_field because Rails will provide one for you. Still unsure why you need a "checked" field, but it seemed to not work without one.
Try something like this:
<%= form_for #user, :url => user_notification_preferences_path(#user), :method => :put do |f| %>
<%= check_box_tag "user[notification_preferences][]", :notify_on_friend_post, #user.notification_preferences.try(notify_on_friend_post) %>
<%= f.submit %>
<% end %>

Hiding Checkbox & Assigning Value - Ruby on Rails - Easy Question

I am trying to hide a checkbox and assign a default value of 1 such that the submit button only shows. Here is my form. Just wondering as the proper format as I am new to rails. I think you can do this with helpers but was wondering if I can just include it in the form. Here is the form:
<% remote_form_for [#post, Vote.new] do |f| %>
<p>
<%= f.label :vote %>
<%= f.check_box :vote %>
</p>
<%= f.submit "Vote" %>
You can certainly do this, but if all you want is to set a parameter without displaying a field, what you probably want instead is a hidden field:
<%= f.hidden_field :vote, :value => '1' %>
If you really do want a hidden checkbox (maybe so you can optionally display it later using javascript?), you can do it like this:
<%= f.check_box :vote, :checked => true, :style => 'visibility: hidden' %>
You could use CSS to hide the checkbox:
<%= f.check_box_tag :vote, 1, true, :style => "display: none;" %>
But if you just want to pass a value you can just use a hidden field:
<%= f.hidden_field_tag, :vote, 1 %>
If you just want to pass the value along, use a hidden field
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field_tag 'vote', '1' %>
<%= f.submit "Vote" %>
<% end %>

Resources