Set the width of a selectbox in Rails 3 - ruby-on-rails-3.1

I need to specify a minimum width for a select box input on a form using form_for(#project).
I have <%= f.select(:category, "", html_options{:size => 30} ) %>, but I'm getting a SyntaxError in the ActionController.
Any ideas?

The select helper takes four parameters. If you want to add an attribute to the HTML tag it generates, that's provided in the fourth parameter. You'll want to do something like this:
<%= f.select(:category, "", {}, {:size => 30}) %>
The html_options name you see in the Rails API is simply the internal parameter name used by Rails to apply those hash values to the tag it generates. You simply need to provide it a hash.
Also, the size of a select box doesn't set the width or minimum width, it describes the number of elements to make visible vertically (See this jsFiddle for an example). So you may actually be looking for something more like this:
<%= f.select(:category, "", {}, {:style => "min-width: 100px;"}) %>

Related

ActionView::SyntaxErrorInTemplate at /template_name

I need to dynamically load a sidebar's content into a sidebar partial based on the presence of a directory name-spaced as the controller_name (if present) and the action_name (always used). They both are derived from the Rails hash and map to the .../views/sidebars/[controller_name]/[action_name] if there is a directory for the controller_name and to .../views/sidebars/[action_name] if there isn't an associated directory namespaced with the Controller's name.
/views
/sidebars
/static
/_about.html.erb
/_contact.html.erb
/...
/_styles.html.erb #outside of any controller structure
I am trying to use the following code to dynamically load the correct sidebar partial:
<%= "sidebars/#{controller_name}".to_s.present? ? yield("sidebars/#{controller_name}/#{action_name}" : yield("sidebars/#{action_name}") %>
This checks to see if there is a directory called by the controller_name and it renders the correct partial based on the structure indicated.
I am getting the following error:
Encountered a syntax error while rendering template: check <div id='sidebar_left'>
<p>Template: [_sidebar-left.html.erb]</p>
<%= render "sidebars/#{action_name}" %>
<%= "sidebars/#{controller_name}".to_s.present? ? yield("sidebars/#{controller_name}/#{action_name}" : yield("sidebars/#{action_name}") %>
UPDATE
Ok so it turns out that ternary operators don't always return the value, particularly if it is a computed string value. In this case, I was expecting the value to return as either sidebars/static/home or sidebars/styles. For some reason I can't explain, ternary operators don't work that way, even though I (thought!) I was returning a string value from the operation.
To get rid of the syntax error, I had to assign the operator to a variable name, in this case sb_name and then render it on the next line. This is the final code:
<% sb_name = "sidebars/#{controller_name}".to_s.present? ? "sidebars/#{controller_name}/#{action_name}" : "sidebars/#{action_name}" %>
<%= render sb_name %>
Now, while this resolved the syntax error, I am now getting a Missing partial sidebars/_about error in the view. The _styles file exists as /views/sidebars/_styles.html.erb, right where it should. Since there is not a folder name-spaced to the controller being called (in this case StylesController), it should be pulling up the file directly underneath the /sidebars/ directory. However, this is not the case.
I found some help on SO here. I wasn't checking the directory/file structure properly. The final syntax is this:
<% sb_name = File.directory?("app/views/sidebars/#{controller_name}") ? "/sidebars/#{controller_name}/#{action_name}" : "/sidebars/#{action_name}" %>
<%= render partial: "#{sb_name}" %>

generating barcode for Barby gem under EAN-13

While attempting to render in HTML a collection of article barcodes and proceeding incrementally to view the data (relative to other objects on tha page), the controller
require 'barby/outputter/html_outputter'
require 'barby/barcode/ean_13'
and the view invokes
<%= this_barcode = article.barcodes.first.barcode %>
<%= this_barcode.class %>
<%# barcode = Barby::EAN13.new(this_barcode) %>
<%# outputter = Barby::HtmlOutputter.new(barcode) %>
<%# barcode.to_html.html_safe %>
[returns as expected]
8001300303466 String
however, when wanting to get the barcode and uncommenting that element it fails to process because data not valid for the line barcode = Barby::EAN13.new(this_barcode) (also occurs with barcode = Barby::EAN13.new(this_barcode.to_i))
note possibly on garden path here, as there is confusion with what the wiki suggests with
barcode = Barby::DataMatrix.new(number) as that would generate the error
uninitialized constant Barby::DataMatrix
How does one ensure the data is correct for proper rendering?
Here's the issue:
8001300303466 has 13 characters. It is the correct barcode.
I was assuming one could submit a correct barcode. However line 54 of the gem's ean_13.rb file
allows a 12 character data element FORMAT = /^\d{12}$/ and proceeds to calculate the check digit.
Thus,
<%= barcode = Barby::EAN13.new(this_barcode[0...-1]) %>
will end up processing the data correctly.

Error in a Phoenix template list

I'm trying to extract values form a list (<%= #evento %>) in a template but I'm getting this error:
lists in Phoenix.HTML and templates may only contain integers representing bytes, binaries or other lists, got invalid entry: %Skeleton.News.Evento{__meta__: #Ecto.Schema.Metadata<:loaded, "news_eventos">, date: "DEZ 2011", id: 69, imgPaths: ["images/fabasa/eventos/one/1.jpg", "images/fabasa/eventos/one/2.jpg", "images/fabasa/eventos/one/3.jpg", "images/fabasa/eventos/one/10.jpg"], inserted_at: ~N[2017-06-30 12:38:15.452214],...
So, my question is how to transform this in a datatype structure that I can still iterate in my template?
You cannot print a list like that in a template because templates only allow iolists to be printed, which are lists containing integers, binaries (also called String in Elixir), or iolists.
If you want to print the inspect representation of the list (the one you see in iex), you can do:
<%= inspect #evento %>
To iterate through the list, you can use for:
<%= for event <- #evento %>
<%= event.id %>
<% end %>

How to calculate and display values from variables in Rails

I am developing a web application with Ruby on Rails/Jquery Mobile.
The SQLite 'Inventory' schema consists of existingfixture:string, replacementfixture:string, and quantity:integer. There are two other tables called oldlights and newlights. These simply contain the options for existingfixtures and replacementfixtures, respectively. Oldlights and newlights both have lightname:string and value:integer, where value is how many Watts the given light name uses.
I also want my program to display, the difference in watts from existing to replacement fixture. I want to be able to have another column saying how many Watts are saved anually, but I have no idea how to go about this. The values are associated in oldlights/newlights. However, I can't figure out how to access them and perform mathematical equations with them in a seperate column.
Code for Displaying values from database:
<fieldset class="ui-grid-c"><center>
<div class="ui-block-a"><%= inventory.existing %></div>
<div class="ui-block-b"><%= inventory.replacement %></div>
<div class="ui-block-c"><%= inventory.quantity %></div>
<div class="ui-block-d"><%= link_to 'Remove', [inventory.client, inventory],
:confirm => 'Are you sure?',
:method => :delete %></div></center>
</fieldset>
Code for adding inventory
<%= form_for([#client, #client.inventories.build]) do |f| %>
<fieldset class="ui-grid-c">
<div class="ui-block-a"><%= f.select :existing, Olight.all.collect{|p| [p.name]}, { :include_blank => true } %></div>
<div class="ui-block-b"><%= f.select :replacement, Nlight.all.collect{|p| [p.name]}, { :include_blank => true } %></div>
<div class="ui-block-c"><%= f.number_field :quantity, "data-mini" => "true" %></div>
<div class="ui-block-d"></div>
</fieldset>
<%= f.submit 'Add Inventory'%>
<% end %>
Sebastien is partly correct. (Parsing the fixture name won't work, since in the "1-4' 40W-T12-ESMB" naming, 40 isn't the wattage of the fixture, but that of a single 4 foot T-12 lamp, and the engineering isn't as simple as the wattage of a "3-4' 40W-T12-ESMB" fixture == 3 times the wattage of a "1-4' 40W-T12-ESMB" fixture. The aggregate fixture wattage depends on a number of other factors in addition to lamp wattage.)
You should read a this page! "Section 3 - Making Select Boxes with ease" for more information on how to setup your drop lists -- I like to construct using options_for_Select. I believe you want the form where the name is displayed, but the id of the old and new fixtures is what is stored in your database.
In your rails controller when you are saving an inventory combination (existing, replacement, and count), you use the old and new fixture ids to look up their wattages, do the math, then store the savings value. When the page is displayed, all the data should be present.
On a style note, I'd move the Olight/Nlight.all.collect code out of the page template and into the controller, setting up instance variables to be used by the view...
#existing = Olight.all.collect{|old| [old.name, old.id]}
#replacement = Nlight.all.collect(|new| [new.name, new.id]}
I thank you have to do many things :
First, you have to get Watts from the new one and the previous one. For that, 2 solutions :
You make a regexp to extract the watt from the name or you add a field on light where the user can specify watts.
Second, on "Add inventory" click, you send an ajax call to your server with extra parameters as "newWatts" and "oldWatts". On create action on server side, you compute the energy saved and you save it in your database.
Do you understand?

Dynamic markup with MVC

I have markup stored in a database, which I am pulling out and placing onto a page.
below is a basic sample of what I have, without any db calls for ease of example;
The controller:
ViewData["testMarkup"] = "I was here <%= DateTime.Now.Year %>";
The View:
<%= ViewData["testMarkup"] %>
now this out puts: I was here
and no date, this is because it is ignoring the <%= %> part, is there anyway I can output the above said string and woudl include the year?
Many thanks,
Just do the following:
ViewData["testMarkup"] = "I was here " + DateTime.Now.Year.ToString();
Or am I missing something? Code blocks, such as <%= DateTime.Now.Year %> are only valid when they are part of the markup:
<div>The year is <%= DateTime.Now.Year %></div>
The markup in the database is being treated as a string, not as code in your view language, so it is simply writing it out as text, c# and all.
Two alternate methods:
1 - Use a templating system, such as
ViewData["testMarkup"] = "I was here #YEAR#";
and have a method that replaces your tokens (e.g. #YEAR#) with their values at render time, e.g.,
<%= ReplaceTokens((string)ViewData["testMarkup"]) %>
Where ReplaceTokens looks like:
public static ReplaceTokens(string s)
{
return s.Replace("#YEAR#", DateTime.Now.Year)
}
2 - Store your markup in a partial view, and save the name of the partial view in the database if necessary.
I do believe Phil Haack has the answer to my issue. http://haacked.com/archive/2009/04/22/scripted-db-views.aspx
I will have to check this out and see what happens

Resources