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 }
Related
I have used an action to display all topics of a course, when that course is selected. Now I want to use virtual attribute to display a field number_question on form for each topic to use can enter a number. My model:
class GeneralExam < ActiveRecord::Base
attr_accessible :course_id, :description
attr_accessor :numbe_question
end
This is my form:
<%= simple_form_for #general_exam, defaults: { error: false } do |f| %>
<fieldset>
<legend>General Exam information</legend>
<%= render 'shared/error_messages', object: f.object %>
<%= f.association :course, collection: current_user.courses, prompt: "Choose Course for exam" %>
<%= f.input :name %>
<%= f.input :description, input_html: { rows: 2, class: 'span6' } %>
</fieldset>
<fieldset>
<legend>Choose number question for topics</legend>
<div id="list_topics">
</div>
</fieldset>
<%= f.submit class: "new_resource" %>
<% end %>
This is my action:
def update_topics
#course = Course.find(params[:course_id])
#topics = #course.topics
end
My update_topics.js.erb
$('#list_topics').html("<%= j (render('general_exams/list_topics')) %>")
My _list_topics.html.erb partial:
<% #topics.each do |topic| %>
<h5><%= topic.name %></h3>
<%# f.input :number_question, input_html: { name: "number_question[#{topic.id}]" } %>
<% end %>
But if I want to display input field, I have to have a f object, but when I render _list_topics, I didn't have. So I want to ask how to pass f object of form to partial _list_topics?
Or someone can tell me how to use jquery to add input field after h5 element on the form, thank so much.
I think there are some different approaches to solve the problem. Two of them (the ones i:
1) You could define a different form to post the number_question data to another controller action.
2) Submit the number_question using jquery's change callback when user finishes updating the field value.
I think first option is better and easier to code. You will end up with something like this:
<%= simple_form_for #general_exam, defaults: { error: false } do |f| %>
<fieldset>
<legend>General Exam information</legend>
<%= render 'shared/error_messages', object: f.object %>
<%= f.association :course, collection: current_user.courses, prompt: "Choose Course for exam" %>
<%= f.input :name %>
<%= f.input :description, input_html: { rows: 2, class: 'span6' } %>
</fieldset>
<%= f.submit class: "new_resource" %>
<% end %>
Then, you'll have a different form on the same template (to submit topic numbers):
<%= simple_form_for #general_exam, url: whatever_is_your_new_topics_number_path, defaults: { error: false } do |f| %>
<fieldset>
<div id="list_topics">
</div>
</fieldset>
<% end %>
You will continue to respond with your JS partial, replacing div's content accordingly to topics data.
So I have a simple form for entering "brand" and "model" through a single submit button, as below:
<%= simple_form_for #brand, :html => { :class => 'form-horizontal' } do |m| %>
<fieldset>
<legend><%= controller.action_name.capitalize %> /Brand</legend>
<%= m.input :name %>
<%= m.simple_fields_for :models, Model.new do |p| %>
<%= p.input :name %>
<% end %>
<div class="form-actions">
<%= m.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', brands_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
I have name:string in my schema for both brand and model.. and validates_presence_of :name in both models.. The form DOES work for creating the brand and model simultaneously but my error "can't be blank" only shows up for the brand field.
Thanks for any help with this issue.
Realized what I was doing wrong. Needed to ALSO put #model = Model.new inside of the controller and put <%= m.simple_fields_for :models, #model do |p| %>
I'm having some troubles when I want to change the class of a form.
I have this partial:
<%= simple_form_for(#activity) do |f| %>
<%= f.input :name %>
<%= f.submit 'Guardar',:class=>"btn success" %>
<% end %>
In the browser the form label has class="simple_form activity"
According to Simple Form documentation, I can change the class by doing this
<%= simple_form_for(#activity, :defaults=>{:class=>"my_class"}) do |f| %>
But nothing is happening, what is wrong?
JavierQ
I think you want to use:
:html => { :class => "btn success"}
Instead of:
:class=>"btn success"
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 %>
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 %>