very simple ruby combobox - text parse prolem - ruby-on-rails

tried to create simple dropdown in ruby - like that :
<%= select_tag(:origin_id, '<option value="1">Lisbon</option>') %>
and I received an empy one.
this is the generated html
<select id="origin_id" name="origin_id"><option alue="1">Lisbon</option>/select>

Mark your HTML as safe to avoid Rails to escape it.
<%= select_tag(:origin_id, '<option value="1">Lisbon</option>'.html_safe) %>

Related

rails options_for_select update form

This is my biblios_helper.rb:
def main_language
[["français","frenchLit"],["latin","latinLit"],["ancien français","froLit"],["néerlandais","dutchLit"]]
end
the form for adding a new bibliography contains :
<%= f.select(:langue_main) do %>
<% options_for_select(main_language, selected: params[:biblio] ? params[:biblio][:langue_main] :"") %>
<% end %>
This results in this html :
<select name="biblio[main_language]" id="biblio_main_language"><option value="frenchLit">français</option>
<option value="latinLit">latin</option>
<option value="froLit">ancien français</option>
<option value="dutchLit">néerlandais</option></select>
That works fine. However, I have the same code in the form that allows for the updating of the bibliography.
when in the database, the language is 'froLit', I want the default to show up in the select menu to be 'ancien français'. How do I do that?
On edit form it will automatically select the corresponding option as default which options is saved in database for this object -
<%=f.select :langue_main, options_for_select(main_language) %>
However if you want to be default selected as always to be ["ancien français","froLit"] Then try this one -
<%=f.select :langue_main, options_for_select(main_language, "froLit") %>
maybe just:
= f.select :langue_main, options_for_select(main_language, params.dig(:biblio, :langue_main))

Rails - input default value from record

Perhaps I'm being silly but I've been trying to set a default value for an input with ruby on rails for hours and haven't cracked it.
I'm making a partial which can either allow users to create new records but will also show existing records if they exist. Code as follows
<input type="text" <%= (#prices.empty? || #prices.first.name.length == 0) ? 'placeholder="General admission"' : "value=" + #prices.first.name.to_s %> >
Which works perfectly for any value that exists UNLESS there is a space, for example, if price.name = "general admission" OR if price.name = "" (in which case it prints the placeholder) I get the following produced
<input type="text" admission'="" value="'general" id="event_price_name" name="[event][price][0][name]" aria-label="..." class="form-control">
It seems to get tripped up by the space.
Am I trying to use Rails in a way it wasn't designed to be done in? I'm more used to PHP which may be it!
Thanks
You should use the text_field_tag helper to build the input, this is preferred over building it with partial interpolation. Also, placeholder will automatically be overwritten if there is a value so you don't need to handle that part in the code, that's how it behaves by default on the browser.
<%= text_field_tag :admission, #prices.first.name.to_s, {placeholder: 'General Admission'} %>

Getting forms to look good in simple_form with Zurb Foundation

I have been using simple_form for a few years now, but always either by itself or with Bootstrap. I'm using Foundation in a new project, and I have followed Zurb's instructions for installing the foundation-rails gem. I have also run:
rails g simple_form:install --foundation
And it has created the requisite files.
Now for the confusing part. When I used to generate a controller or a scaffold with simple_form for Bootstrap, it would make the form look great by default.
But when I run those same generators in my new Rails project using simple_form for Foundation, the forms don't appear to be styled at all. The fields stretch all the way across the screen.
Passing in the wrapper HTML classes that are specified in the config/initializers/simple_form_foundation.rb file don't do anything.
For example:
<%= simple_form_for(#organization, html: { class: "horizontal-form" }) do |f| %>
I can see in the HTML source that it is indeed putting "inline-form" as a class on the form, but there's nothing else going on. None of the wrapper stuff around any of the divs is different, it's just plain:
<div class="input string optional organization_official_name">
<label class="string optional" for="organization_official_name">Official Name</label>
<input class="string optional" id="organization_official_name" name="organization[official_name]" type="text" value="Voolith">
</div>
I have not done anything fancy in my application layout yet, other than the nav bar which is using Zurb classes and works perfectly.
I think I am confused on what exactly simple_form is supposed to do here? I don't understand the connection between the configuration in the simple_form.rb and simple_form_foundation.rb files and CSS classes.
Right now, passing the "vertical-form" or "horizontal-form" classes as I have done above doesn't do anything. Isn't simple_form somehow supposed to read that value and render the form differently?
I had this same problem and googling didn't yield quick answers.
I believe instead of
<%= simple_form_for(#organization, html: { class: "horizontal-form" }) do |f| %>
try
<%= simple_form_for(#organization, :wrapper => :horizontal_form) do |f| %>
This references the config.wrappers in config/initializers/simple_form_foundation.rb

grails fields plugin widget String is escaped

I am using Grails 2.3.7 and Fields plugin version 1.3
With this in my _field.gsp under _fields folder
<%# page defaultCodec="html" %>
<div class="control-group ${invalid ? 'error' : ''}">
<label class="control-label" for="${property}">${required? '*' : '' } ${label}
</label>
${widget}
</div>
The input tags are getting escaped and hence showing up as Strings instead of being rendered as html components.
I tried decoding them using widget.decodeHTML() but doesnt seem to make a difference.
Is there any other configuration I am missing to make this work?
In Grails 2.3.x you can use the raw() method in views to specify that your output is safe and should not be escaped. In your case:
${raw(widget)}
This blog post by Mr. Haki should be helpful.

how to put place holder for <%= f.datagrid_filter filter%>?

I made a rails application, and I used datagrid gem to handle filters, pagination,and orders(ascending and descending). I was supposed to write <%= f.datagrid_filter filter%> to filter according to a filed of the table(Ex:title field in topics table).
Now <%= f.datagrid_filter filter%> returns a traditional html input tag like below. <input id="topic_report_title" class="title string_filter" type="text" size="30" name="topic_report[title]"> in the html console.
Now I want to put placeholder in that helper method only.
Can anybody help please?
Have you tried to do the following: <%= f.datagrid_filter filter, :placeholder => "placeholder text"%>

Resources