Rails 3, checked radio button with if function - ruby-on-rails-3

I have a radio button within my form as follows
<div class="btn-group" data-toggle="buttons-radio">
<%= f.radio_button :start_year, :class=> "btn", :value=> '2007' %> 2007
<%= f.radio_button :start_year, :class=> "btn", :value=> '2008' %> 2008
<%= f.radio_button :start_year, :class=> "btn", :value=> '2009' %> 2009
</div>
I am using twitter bootstrap
I want to do something like the following:
<div class="btn-group" data-toggle="buttons-radio">
<%= f.radio_button :start_year, :class=> "btn", :value=> '2007', if #dates.start_year == 2007 :checked => true end %> 2007
<%= f.radio_button :start_year, :class=> "btn", :value=> '2008', if #dates.start_year == 2008 :checked => true end %> 2008
<%= f.radio_button :start_year, :class=> "btn", :value=> '2009', if #dates.start_year == 2009 :checked => true end %> 2009
</div>
But I get the following error:
syntax error, unexpected keyword_ensure, expecting ')'
syntax error, unexpected keyword_end, expecting ')'
I must be making a mistake in the if statement within the radio button, but I'm not sure how exactly to correct this

Try
<%= f.radio_button :start_year, :class=> "btn", :value=> '2007', :checked => Proc.new { #dates.start_year == 2007 ? true : false } %> 2007

I know this is an old question, but maybe it helps others.
I don't think we need Proc here, we can just pass boolean value to :checked key.
Also, #dates.start_year == 2007 ? true : false can be simplified to #dates.start_year == 2007.
So, the result will be as simple as
<%= f.radio_button :start_year, :value=> '2007', :checked => #dates.start_year == 2007%> 2007

Related

Rails bootstrap-datepicker-rails not working

I have a Rails 3.2.19 app using twitter-bootstrap-rails 2.2.6 and bootstrap-datepicker-rails 1.3.0.2.
I am trying to get a text_field in a form to display the datepicker but nothing happens when I click or focus on the field.
When I inspect the element I see the following:
<input data-behaviour="datepicker" id="project_due_date" name="project[due_date]" size="30" type="text">
When checking the console I do not receive any JS/Coffee errors.
Here is my code:
projects.js.coffee
$(document).on "focus", "[data-behaviour~='datepicker']", (e) ->
- $(this).datepicker
- format: "yyyy-mm-dd"
- weekStart: 1
- autoclose: true
projects/_form.html.erb
<%= form_for(#project, :html => { :class => "well"}) do |f| %>
<% if #project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#project.errors.count, "error") %> prohibited this contact from being created:</h2>
<ul>
<% #project.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :project_name %>
<%= f.text_field :project_name, placeholder: "Build Spreadsheet", required: true %>
<%= f.label :project_manager %>
<%= f.collection_select(:user_id, User.all, :id, :full_name)%>
<%= f.label :due_date %>
<%= f.text_field :due_date, 'data-behaviour' => 'datepicker' %>
</br>
<%= f.button :submit, :class => 'btn btn-info' %>
<% end %>
I've tried also calling it based on id and have had no luck. I see the same behavior in Chrome, Firefox, or Safari.
Any help is greatly appreciated.
It looks like my CoffeeScript was wrong. I changed it to the following:
$ ->
$('[data-behaviour~=datepicker]').datepicker(format: "yyyy-mm-dd", autoclose: true)
Now I'm able to select the field and the datepicker comes up.

Rails - Ransack gem - radio buttons settings

I am using Ransack gem in order to create simple search interface. One of the model attributes can be true or false. So, using the Ransack form builder I have the following code:
<div class='radio'>
<%= f.label :is_active_true, 'Is active' %>
<%= f.radio_button :is_active_true, 1, checked: true %>
<%= f.label :is_active_false, 'Is not active' %>
<%= f.radio_button :is_active_false, 1 %>
</div>
The problem is I am not able to "unchecked" the radio buttons, once they are checked.
Is there any optios to make the buttons to work or I should use additional JavaScript?
Like this?
<div class='radio'>
<%= f.label :is_active_true, 'Is active' %>
<%= f.radio_button :is_active_true, 1, checked: true %>
<%= f.label :is_active_true, 'Is not active' %>
<%= f.radio_button :is_active_true, 0 %>
</div>

Rails view text_field existing value vs defaults

I am looking for a more elegant way to accomplish this.
<div class="control-group">
<%= f.label :shoot_date, class: "control-label" %>
<div class="controls">
<% if #shoot.new_record? %>
<%= f.text_field :shoot_date, :class => 'datepicker', :value => Date.today.strftime('%m/%d/%Y'), 'data-behavior' => 'datepicker', :readonly => true %>
<% else %>
<%= f.text_field :shoot_date, :class => 'datepicker', :value => #shoot.shoot_date.strftime('%m/%d/%Y'), 'data-behavior' => 'datepicker', :readonly => true %>
<% end %>
<span class="help-block">Sitting date of this shoot.
</div>
Yeah, just throw it in the controller:
def new
shoot.shoot_date = Date.today
end
You shouldn't have to put it in the edit method or anything; that should happen automatically. You could even put it in the model if you like, but the controller works well.

RoR: why am I getting undefined method 'hidden_field_tag'?

right now I have two forms in a row
<section>
<%= render 'shared/micropost_form_purchase' %>
<%= render 'shared/micropost_form_sale' %>
</section>
then for _micropost_form_purchase.html.erb
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and for _micropost_form_sale.html.erb I have
<%= form_for(#micropost, :html => { :id => "sale" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
so I want the first micro post to automatically become a purchase micropost (I have a column in the micropost database called type that is a string that I want to depict either sale or purchase) and for the second one I want it to become a sale micropost. I was using hidden_field_tag because I thought you didn't have to define it in the controller, but am I wrong? Is hidden_field more appropriate? how can I use hidden_field_tag?
You can use:
<%= f.hidden_field :type, :value => "sale" %>
or:
<%= hidden_field_tag 'micropost[type]', "sale" %>
but not:
<%= f.hidden_field_tag :type, :value => "sale" %>
Using f.hidden_field will use the value from the variable #micropost, whereas hidden_field_tag will not use that.
It should be f.hidden_field not f.hidden_field_tag as you're using the model's form helpers :)

Rails3 acts_as_taggable with a form

I've just started working with the acts_as_taggable gem. Really liking it so far, but I am a bit unclear about how to use this gem with a form.
class Photo < ActiveRecord::Base
acts_as_taggable_on :tags
end
In my form for Photos I am trying to implement a series of checkboxes for the user to assign tags to their photo:
<%= f.label :tag_list %>
<%= f.check_box :tag_list, "landscape" %>
<%= f.check_box :tag_list, "people" %>
When viewing the form I get this error:
NoMethodError in Photos#edit
...line #19 raised:
undefined method `merge' for "landscape":String
Extracted source (around line #19):
18: <div class="float_tag">
19: <%= f.check_box :tag_list, "landscape" %>
Any thoughts as to how I should create my form?
I'm assuming your <form> looks something like this:
<%= form_for(#photo) do |f| %>
<%= f.label :tag_list %>
<%= f.check_box :tag_list, "landscape" %>
<%= f.check_box :tag_list, "people" %>
<% end %>
You should change up your f.checkbox lines a bit:
<%= form_for(#photo) do |f| %>
<%= f.label :tag_list %>
<%= f.check_box :tag_list, { :multiple => true }, 'landscape', nil %>
<%= f.check_box :tag_list, { :multiple => true }, 'people', nil %>
<% end %>
Which will post something like this when submitted (with only people selected, for example):
{ :post => { :tag_list => ['', 'people'] } }
For anyone trying to get this to work with Rails 4 and strong parameters, I also had to permit the the tag_list param as an array.
params.require(:clip).permit(
:name, :other_params, { tag_list: [] }
)