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

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.

Related

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

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!

Use a variable name in an erb include call

I'm using Sinatra and I'd like to include another erb file in one of my views but using a variable for the file name.
I was thinking something like this may work but I get an error saying no such file could be found.
<%= erb :'layout/nav/#{device_type}' %>
Currently I'm using the below switch statement to achieve the desired results but the above would be cleaner and less code.
<% case device_type
when 'mobile'%>
<%= erb :'layout/nav/mobile' %>
<% when 'tablet' %>
<%= erb :'layout/nav/tablet' %>
<% else %>
<%= erb :'layout/nav/desktop' %>
<% end %>
Thanks
Giles
I think that could work, you just need to use double quotes to interpolate strings.
Try
<%= erb :"layout/nav/#{device_type}" %>

Adding class to image_tag Ruby

Probably something really simple but keep getting syntax errors.. I want to add a class to my image_tag, which is using a helper to form part of a url , though didnt think this would effect where i put my class.
<%= image_tag(aws_asset "/assets/img/thumb-1.jpg"), :class => "stock" %>
Can anyone see anything obvious that im missing.
Thanks
Try this:
<%= image_tag(aws_asset("/assets/img/thumb-1.jpg"), :class => "stock") %>
Edit: Your original code was calling the image_tag function with the result of aws_asset "/assets/img/thumb-1.jpg as the only parameter (the :class => "stock" bit was left out). The additional parentheses change call to the image_tag function to have two parameters, the first being the result of aws_asset("/assets/img/thumb-1.jpg") and the second being :class => "stock".

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.

How do I perform inline calculations on two variables within an .erb file?

I have the following .erb view in a Sinatra app:
<% sessions.each do |session| %>
<%= session.balance_beginning %>
<%= session.balance_ending %>
<% end %>
It works as expected, displaying the beginning and ending balances recorded for each session. I would like to calculate the net balances from within the .erb file, but I can't figure out how to do it. I have tried variations of this:
<% sessions.each do |session| %>
<%= session.balance_ending - session.balance_beginning %>
<% end %>
That doesn't work. I receive the following error in Sinatra:
undefined method `-' for nil:NilClass
How do I do what I'm trying to do?
Right #Zabba, in this case I think you would add a method to your Session model so you could call session.net_balance.
Then in your balance_ending and balance_beginning methods you would want to handle nil, either raise an error or return zero if that is valid.

Resources