How to insert <br> after label tag in simple_form? - ruby-on-rails-3

Using simple_form, I have a simple input like this:
<%= f.input :email %>
This generates (removed irrelevant parts):
...
<label>Email</label>
<input .... />
...
I want the input control to come below the label, so I need a <br/> after </label>:
...
<label>Email</label>
<br/>
<input .... />
...
How do I do the above?
I tried:
inserting a <br/> in config/initializers/simple_form.rb:
config.label_text = lambda { |label, required| "#{required} #{label} <br/>" }
And this generates:
...
<label>Email<br/></label> #note the location of <br/>
<input .... />
...
The <br/> is just before the </label>. Now, this works in terms of showing the input on the next "line", but it just feels "wrong".
Is there some way to do this properly?

It's usually better to do this with stylesheet rules than by inserting HTML tags, since the break is a matter of formatting rather than of content. It could be something as simple as...
label {
display: block;
}

Totally agree you should use css to do this as Steve described. But if you really want to, you can still do this by using rails' default form helper like so:
<%= f.label :email %>
<br>
<%= f.text_field :email %>

Related

Specifying jquery mobile data themes within a Rails 3.0 form

I would like to specify a jquery mobile theme for an element in a rails 3.0 form. The following doesn't work.
<div data-role="page" data-theme="a">
... #content formatted with data theme a
<div data-role="checkbox" data-theme="c">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
... #content formatted with data them a
</div>
Thanks!
UPDATE: shanabus solution in rails format -
<%= f.label :remember_me, { 'data-theme' => 'c' } %>
In order for you to specify the theme of a checkbox in jQuery Mobile, you would have to set it directly on the checkbox element. Could be something like this:
<%= f.check_box("label", "accepted", { 'data-theme' => 'c' }, "yes", "no") %>
Here is the docs for jQuery Mobile: http://jquerymobile.com/test/docs/forms/checkboxes/options.html
And for a Rails example of adding attributes to a check_box: http://apidock.com/rails/ActionView/Helpers/FormHelper/check_box
Hope this helps!

How to render a select tag with an option marked as selected

<div class="field">
<%= f.label :category %><br />
<%= select_tag "category", options_from_collection_for_select(#categories, "id", "name"), :prompt => "Select something" %>
<% if not #category_id.nil? %>
<script>
$("#category option").each(function(){
if ( this.value == <%= #category_id %> )
this.selected = true;
});
</script>
<% end %>
</div>
I wanna make the category <select> tag have the right value (#category_id).
I try adding some script to category.js.coffee, but I found I can't pass the #category_id to the coffee file. So the script is inline.
I don't know any other ways to solve the problem. I just think my code is ugly.
Can anybody have any other solutions about my question.
If you were me, which method will you use. Thx.
Regards,
Rails newbie
You should be able to render one of the options as pre-selected without javascript. Try options_from_collection_for_select(#categories, "id", "name", #category_id).

Conditional link display in rails

I have a filter bar on my page. The bar should always be in place, however only when I'm on the detail page I want to show a link <- back to list inside of it. Otherwise the filter bar should be empty. What is the most elegant way of doing this in rails 3 or 3.1?
Thanks
Thomas
To return to previous page you can use link_to "Back", :back
To show or hide the link you can use the controller_name and action_name methods with a if/unless conditional.
From your question and the comment, you have the following structure:
application.html.erb:
...
<section id="filter-bar">
<section id="filter"></section>
</section>
I see there two different options how to include your link conditionally:
By doing an if-then in your file application.html.erb
By including a yield with a symbol that denotes the context.
Here is the pseudo-code for that:
solution
application.html.erb:
...
<section id="filter-bar">
<section id="filter">
<% if controller_name == 'user' && action_name == 'show' %>
<%= link_to "Back", :index %>
<% end %>
</section>
</section>
solution
application.html.erb:
...
<section id="filter-bar">
<section id="filter">
<%= yield(:filter) %>
</section>
</section>
view.html.erb:
<%- content_for :filter do %>
<%= link_to "Back", :index %>
<% end %>
...
index.html.erb:
// No content_for section in the file, so it will be empty here.
The first solution is simpler, much more condensed, but all the information if something is included or not is in one file. If that is changed a lot, that may be a hotspot in your application. The second is more object-oriented, but perhaps more to change and think about. But both will working for you.

Weird rendering of .html.erb file

So I have this file:
<h1>Calendar view</h1>
<div class="events">
<% #events.each do |e| %>
<%= raw(e.content)%>
<% end %>
</div>
<br />
<div class="messages">
<% #messages.each do |m| %>
<%= raw(m.content)%>
<% end %>
</div>
With #events and #messages being valid instance variables in the controller...but when I go to the page the html looks like this:
<h1>Calendar view</h1>
<div class="events">
<br>
<div class="messages">
This is another message test
</div
Event Content
</div>
I'm confused. Maybe I'm missing something obvious?
The problem is that raw() will output raw HTML content. The Rails template engine will try to merge that with the .erb template you supplied.
Therefore, if either m.content or e.content are malformed, you will most likely get unexpected output.
The best way would be to look for syntax errors, especially missing closing elements.

Translating label for references collection_select

i have form with one selectbox (collection_select) for references column. What is proper way to translate label for this widget? For all others a use default Model.human_attribute_name (my code, Translations for Active Record Models)
If you use in your [language].yml file:
attributes:
my_model:
property: 'translatet property label'
[...]
You can simply use the following in your view:
<%= form_for([...]) do |f| %>
<div class="field">
<%= f.label :property %><br />
<%= f.collection_select [...] %>
</div>
[...]