Adding class to image_tag Ruby - 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".

Related

Add class to erb element

<%= d.box, :class => "something" %>
What is my syntax error here? Can't figure it out. Class is not properly adding to erb variable.
Error message:
syntax error, unexpected =>, expecting :: or '[' or '.' ...pend=(
d.box, :class => "something" );#output_buffer.safe... ... ^
You can't add class to a simple value (d.box). ERB will render the value of d.box, let's say 123. So you are trying to add class to 123 which is not a html element. You should add class name to the parent html element.
<%= %> means just run code and print on view.
so, <%= d.box, :class => "something" %> raise error.
I think you tried call helper method.
Show document about TagHelper

String will work but not symbols in Rails

To learn Rails, I am writing a simple guestbook app that does not use a database.
Anyway, this is what my view looks like:
views/guest_book_pages/home.html.erb
<h1>Guest Book</h1>
<%= #userinput %>
<%= form_for(:guestbook) do |f| %>
<%= f.label :input %>
<%= f.text_field :input %>
<%= f.submit "Sign" %>
<% end %>
And the controller looks like this:
controllers/guest_book_pages_controller.rb
class StaticPagesController < ApplicationController
def home
#userinput = params[:guestbook]["input"]
end
end
Whenever I change the "input" to a symbol :input, the application breaks and gives me a warning that says: undefined method `[]' for nil:NilClass
What is the reason for this? Why can't I use a symbol?
update: Now it won't even work with the string. What is going on?
update#2: It works with both symbols and string. The only problem is that it will not load the first time. If I can get the page to load, then either will work. How can I get the page to load?
Action use to be handle something, and render view.
when you inter home, the home action has be called, and no param posted now.
for your code, home action should just be empty, it just to render the home_page.
your handle code should move to some action like sign_in, whitch handle the form post and you can get the params.
The first time you load the page the params var is not set. It is only when you submit your form back that there are params
Try
#userinput = params[:guestbook]["input"] || ''
which will initialize the #userinput to an empty string if the params is not found
edit:
This will check if the params has the key guestbook first, then will either set the instance var userinput to an empty string or the value of [guestbook][input] if it exsists.
If all else fails, the instance var is initialized to an empty string to prevent an error in your view.
if params.has_key?(:guestbook)
#userinput = params[:guestbook]["input"] || ''
else
#userinput = ''
end

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.

Rails 3 - validation rules and HTML tags in the message

validates_presence_of :city, :message => "City is
required item!"
I would like to ask you if is possible to insert some HTML tags into the error message, for example if I would want to have a word "City" bold.
Thank you
If you're using traditional 'errorExplanation' div in the beginning of the page, you can use something like this:
<% #some_entity.errors.full_messages.each do |msg| %>
<li><%= msg.html_safe %></li>
<% end %>
In the other case, if you're using ActionView::Base.field_error_proc, you can past html_safe in appropriate place of that code.
Try :message => "<b>City</b> is required item!".html_safe.

Resources