I want to change default label YES NO of my checkbox radio_button from my f.input (form_for) - ruby-on-rails-3

I'm starting using Rails.
In my form_for, I want to change label of default value YES / NO (which appear in my front view)
<div class="row">
<div class="col-sm-6">
<%= f.input :experience,
as: :radio_buttons,
label:"A t-il déjà saillie ?" %>
</div>
</div>
Which option should I add to change label ?
How can I display option in one line ?
Thank you for your help

Are you using simple_form_for? I think so from as: :radio_buttons. If you are, try:
<%= f.input :experience, as: :radio_buttons, collection: [['0', 'false'], ['1', 'true']], label_method: :second, value_method: :first %>
You can probably infer the logic from that line. Make an array of arrays for your collection, each sub array containing first the value of the radio button, and then the label.

Related

Rails dropdown/select location objects with individual params

My scenario is on the home page I want to click a dropdown toggle/select. In that toggle I want it to display a list of Location objects. However I want to pass individual params that are different in each one. My goal is to use a dropdown to toggle location information in the view. The problem is right now I'm hard coding locations into this drop down. But I am creating Location objects on a different page. I want those Location objects to populate the dropdown on the home page, but allow the user's choice to pass params to change the view. This would allow an unlimited number of locations. What is a good way to accomplish this.
index.html.erb
**I erased the dropdown code to simplify things.**
<li><%= link_to "#{#location1}", {:location1 => "location1"}%></li>
<li><%= link_to "#{#location2}", {:location2=> "location2"}%></li>
home_controller.rb
if params[:location1]
#current_location = Location.last
#myreviews = Review.where("location_id = ?", #current_location.id).order('created_at asc')
end
if params[:location2]
#current_location = Location.first
#myreviews = Review.where("location_id = ?", #current_location.id).order('created_at asc')
end
SOLUTION
Here is what I did to solve the problem in the end.
View
<ul class="dropdown-menu loc-drop scrollable-menu" role="menu" aria-labelledby="menu1">
<% #locations.each do |u| %>
<li><%= link_to u.name, :params => {:loc => u.id} %></li>
<% end %>
</ul>
</ul>
Controller
#locations = Location.all
if params[:loc]
#chosen_location = Location.where('id = ?', params[:loc]).pluck(:id).first
end
At that point I'm using #chosen_location to display details like name and address.

passing parameters between views and controllers ruby 1.8.7

i have this form nested in view 'A'
<div>
<%if current_user%>
<%=form_for(ModelB.new) do |f|%>
<%if (params[:param_to_check].present?)%>
<%f.hidden_field "param1",:value=> #modelA.id%>
<%f.hidden_field "param2",:value=> current_user.id%>
<%end%>
render button
<%end%>
<%else%>
render other butotn
<%end%>
</div>
and this 'find' in Bs_controller:
#modelA=ModelA.find_by_id(params[:param1])
:option_from_specific_gem=> -(#modelA.wanted_value).abs
when i look to the params passed to the Bs_controller i see:
Parameters: {"authenticity_token"=>"some_hash=", "utf8"=>"✓", "y"=>"42", "x"=>"144"}
and i need to pass the hidden_field's valies to Bs_controller.
thx for the atention :)
here is the gist: https://gist.github.com/rmatuoka/dd6904809cf89cf916f7
I made some comments, I'm gonna go ahead and make them into an answer.
There are 3 possible things. Assuming it's not a typo, you aren't currently outputting the hidden fields to the HTML. You need to us <%= %> to output the result.
<%= f.hidden_field "param1",:value=> #modelA.id %>
<%= f.hidden_field "param2",:value=> current_user.id %>
This is likely the issue. Otherwise I'd guess either current_user or params[:param_to_check] is nil.
Add param1 and param2 to your list of permitted parameters. In your controller, you should have something like:
def modela_params
params.require(:modela).permit(:x, :y, :param1, :param2)
end

Rails - label form helper insert html tags inside content

If I want to insert a <br/> as part of the markup inside the label form helper, how do I do it?
Right now when I do this:
<%= f.label(:foo, "Foo <br/> Bar: ") %>
It outputs Foo <br/> Bar as the text of the label.
What I want is the label to have the line break between Foo and Bar. Like this:
Foo
Bar
The string needs to be marked as HTML safe so that Rails will not sanitize the output. Here is the concise way to write the label.
<%= f.label :foo, 'Foo <br /> Bar: '.html_safe %>
Use the raw method
<%= f.label(:foo, raw("Foo <br/> Bar: ")) %>

simple_form radio button (need to add <span> tag inside of <label>

What is the best way to create the following HTML using simple_form_for gem?
<label>
<input name="form-field-radio" type="radio" />
**<span class="lbl"> radio option 2</span>**
</label>
Note that by default when I create a radio buttons using the following statements, the above is not created. How can I add that tag in?
<%= f.input :state, :collection => Project::STATES, :as => :radio_buttons %>
I had a similar need (to embed a <span> within the <label>). It isn't the cleanest solution but it did work and I think with some tweaking it could get you the ability to have your input and span embedded within the label. The following modification results in:
<label>Name:
<span class="hint">this is a hint...</span>
</label>
I added the following as an initializer (using rails 4 and simple_form 3) to override the label_text method:
# initializers/simple_form_custom.rb
module SimpleForm
module Components
module Labels
def label_text
if hint
hint_text = %[<span class="hint">#{hint}</span>]
else
hint_text = ""
end
SimpleForm.label_text.call(raw_label_text, required_label_text, hint_text).strip.html_safe
end
end
end
end
Then in initializers/simple_form.rb I have:
config.label_text = lambda { |label, required, hint| "#{label}: #{required} #{hint}" }

Rails 3 - how can I customize text-label in label helper?

This is probably a bit low question, but is there any elegant way, how to change the label-text in label helper?
= f.label :name
generate
<input id="car_name" name="car[name]" size="30" type="text">
If I would like to have the text in label, say, Your Car instead of Name, how to do that?
One way is to write the label tag directly as HTML, but this is a bit dirty way...
Just add a second string argument to f.label, like this:
label_tag 'name', 'Your name'
# => <label for="name">Your Name</label>
See here