How to add a class to a number_field bound to model object - ruby

No idea why this isn't working, have read through a lot of doc examples, most of those focus on text_field, maybe number_field is weird? I know that number_field doesn't accept a length option...
<%= form_for #person do |p| %>
<%= p.number_field :zipcode, class: "short-number-field" %>
...
<% end %>
The html never renders with the class, how do I do this?
Note, this is not a number_field_tag, so the number of arguments is 2.
Thanks!

Related

Conditional HTML attribute

In ActionView I need to display an attribute based on a condition.
<%= f.text_field :regmax_remote, {
:class => 'span2',
:style => "display:#{#event.regmax_remote.present? ? "block" : "none"};"
}
%>
Is there a prettier way to go about this?
The above code is fine, If you are going to use it only once in the,
But If this will be used in many places then u may need helper
def event_display_style event
event.regmax_remote.present? ? "block" : "none"
end
if you have multiple attributes based on several conditions then u can use the helper to return the attributes in hash format and use it like this.
<%= f.text_field :regmax_remote, event_display_style(#event) %>
if u want a variable hash with default hash then u can do something like this as well
<%= f.text_field :regmax_remote, {class: "span2"}.merge(event_display_style(#event)) %>
There are some other ways to make this code look better. U may also like the draper gem. which gives an object oriented control over displaying at the same time can acce view helpers.
https://github.com/drapergem/draper
You can try like the following,
<% if (#event.regmax_remote.present?) %>
<%= f.text_field :regmax_remote, class: "span2" %>
<% end %>
Do not copy the same, just edit as per your code and use this as the example.

In carrierwave, using simple_fields_for, iterating on a symbol (:pictures), how do I find the symbol for the image URLs?

I have a _form which renders another form with simple_fields_for. Everything works fine except I can't figure out the symbol that holds the different URLs, I particularly want the thumb URL.
For example, picture.image_uploader_url — but I don't have the actual object, just the :pictures symbol which is being iterated on. So far :image_uploader_url.to_s doesn't give anything useful and I realized this is because that will always just give the symbol name.
Super form:
<%= simple_form_for(#project) do |f| %>
[...]
<%= f.simple_fields_for :pictures do |builder| %>
<%= render "pictures/form", f: builder %>
[...]
Sub form (pictures/_form):
<% unless [:image_uploader_url].nil? or [:image_uploader_url].empty? %>
<%= image_tag(:thumb_url.to_s) %>
<% end %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.input :image_uploader, as: :file %>
<%= f.hidden_field :image_uploader_cache %>
How do I know in general what symbols I will have access to and how do I use them? maybe I dont have them?
EDIT: I don't have 'picture' to work with, only ':pictures' which I'm struggling to find what all I can get from this (i.e. picture has an image_uploader which I can get image_uploader_url(:thumb) but I can't do this with symbols, nor do I know how to get the picture that is the current iteration).
To get the thumb url for picture, just do this: picture.thumb.url. To get the original url, picture.url would do fine.
As far as I could figure out there is no way to get to the individual pictures that are related to the :pictures symbol. I ended up just scrapping the symbol and passing local variables (I was trying to avoid having the controller from making more information directly available).
I still don't have a satisfactory answer but this is the best I could come up with.

How to create a sequence of numbers in a Ruby on Rails form

I have 10 form fields and I want to append a number next to each field, going sequentially from 1 to 10. The problem is that the code is already in a loop. It's inside a partial, and the partial is passed a collection.
<%= fields_for "list", f do |f| %>
<!-- I want 1, 2, etc to appear here depending on the iteration. -->
<%= f.label :name %>
I tried using <%= i += 1 %> but it does not work since i is not defined. If I define i, it will keep getting reset to the same number, so it makes no difference. Any ideas?
Rails automatically defines a local variable called partialname_counter where, obviously, "partialname" is the name of your partial. So if your partial is called e.g. _list_item.html.erb you could write it like this:
<%= fields_for "list", f do |f| %>
<%= list_item_counter + 1 %>
<%= f.label :name %>
<% end %>
(The + 1, of course, is there because the counter starts at 0.)
Another option would be to just let the browser do the numbering for you using an ordered list:
In the view:
<ol>
<%= render :list_item, :collection => #some_items %>
</ol>
...and in the partial:
<%= fields_for "list", f do |f| %>
<li>
<%= f.label :name %>
</li>
<% end %>
This option is probably more semantic, and lists are easy to style in CSS.

Sorting and manipulating a hash in Ruby

In my rails 3.1 project I have a Book model that has a ID, NAME, and BOOK_ORDER. I am using the ranked-model gem, which for its sorting process creates large numbers in the sorting(:book_order) column. Im looking for some help to create a method to sort all of the Books by the :book_order column, then simplify the :book_order numbers.
So, I have this:
controller
#books = Books.all
view
<% #books.each do |book| %>
<%= book.book_order %>
<% end %>
# book1.book_order => 1231654
# book2.book_order => 9255654
# book3.book_order => 1654
But want this:
view
<% #books.each do |book| %>
<%= book.clean_book_order %>
<% end %>
# book1.clean_book_order => 2
# book2.clean_book_order => 3
# book3.clean_book_order => 1
Additionally, i don’t want to change the database entry, just use its current values to make simpler ones.
Thanks!
UPDATE:
Thanks to nash’s response I was able to find a solution:
In my Book Model I added the clean_book_order method:
class Book < ActiveRecord::Base
include RankedModel
ranks :book_order
def clean_book_order
self.class.where("book_order < ?", book_order).count + 1
end
end
<% #books.each do |book| %>
<%= book.book_order_position %>
<% end %>
EDIT:
Oh, I see. https://github.com/harvesthq/ranked-model/issues/10

hash enumeration and using outside arguments

I have a form builder and the normal |f| variable. In the middle I'd like to iterate over some hash and get a list of keys by which to build the form. How can I incorporate "f" as an argument to the enumerator.
<%= form_for ..... do |f| %>
<% available_types.each do |k,v| %>
<%= f.text_filed :selection, :value => v %>
<% end %>
<% end %>
As you can see in the code above, "f" is outside of the scope. Ideas?
f should still be in the scope. You misspelled text_field though.

Resources