Rails - label form helper insert html tags inside content - ruby-on-rails-3

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: ")) %>

Related

I want to change default label YES NO of my checkbox radio_button from my f.input (form_for)

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.

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

Display clicked image in different div Ruby on Rails

I have some images. When an image is clicked I want to get that image displayed in a different div. Been struggling with how to solve this. This is what I've got so far.
<% #user.images.each do |image| %>
<%= link_to image_tag((image.url), :id => 'test')%>
<% end %>
js.coffee
$('#test').click ->
val = $(this).attr("src")
alert val // /assets/image.png
Firstly, since you have an array of images, you should use class than id.
<% #user.images.each do |image| %>
<%= link_to image_tag((image.url), :class => 'test')%>
<% end %>
And in JS, you will bind click event to image with class 'test'.
$(".test").click ->
val = $(this).attr("src")
$("#showDiv").html "<img src=\"" + val + "\" />"
Where #showDiv is the id of the div which you want the image to be shown.

Use translation for submit button

I don't want to use the default
<%= f.submit %>
and have created a helper function for it, which also embeds an icon. The helper function expects a label to put on the newly created button.
I'm calling it like this:
<%= submit_button("icon-plus", I18n.translate("helpers.submit.create")) %>
But now on this text appears on the button:
%{model} toevoegen
Instead of:
Product type toevoegen
If I use the normal submit button then the correct text appears so my yml files are correct. How can I get the correct text to use in the helper?
Helper code:
def submit_button(icon, label)
link_to "javascript:void(0)", :class => 'btn btn-primary', :onclick => "$(this).closest('form').submit()" do
raw('<div class="') + icon + raw(' icon-white"> ') + label +raw('</div>')
end
end
As the I18n guide says, the translate function interpolates variables passed in the %{} brackets using its second argument (a hash).
In your case you need to tell it the model by doing this:
I18n.t("helpers.submit.create", model: "Product type")
If you want a generic option that would work for any model, you can see how Rails itself does it by looking at the source on GitHub - it's something like
I18n.t("helpers.submit.create", model: f.object.class.model_name.human)
As an aside, you don't need to (and probably shouldn't) use raw there. What you are trying to achieve could easily be done with the built-in helpers:
link_to ... do
content_tag :div, label, class: "#{icon} icon-white"
end

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