I'm looping through errors in a user#update using the following HAML:
- if #user.errors.any?
.alert.alert-error
%ul
= #user.errors.full_messages.each do |msg|
%li= msg
However, instead of just displaying the message, I also get square brackets:
First name can't be blank ["First name can't be blank"]
What am I doing wrong?
EDIT:
If I do p msg then the console tail shows only the message, but within the <li> the brackets still render.
I figured it out.
The line:
= #user.errors.full_messages.each do |msg|
should have read:
- #user.errors.full_messages.each do |msg|
Haml was printing out the array on top of doing the loop.
Related
I'm trying to create a search form in my rails application. I've looked up various solutions but they make little sense to me.
I'm getting the following error when I run a search through a form in my rails app. Right now my concern (other than the error) is my instance variable #computers in my index action. I'm pretty sure it's not 'the rails way' to get a search done properly and would love some advice.
Error
undefined method `%' for #<Array:0x5780460>
Parameters after Search
http://localhost:3000/computers?utf8=%E2%9C%93&direction=&sort=&search=bob
Search Form
<%= form_tag computers_path, method: "get" do %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Go", name: nil, class: "btn btn-primary" %>
<% end %>
Call to Method
def index
#computers = Computer.where(school_id: current_user.school_id).search(params[:search]).category(params[:category]).order(sort_column + " " + sort_direction)
end
Method
def Computer.search(search)
if search
search = search.downcase
params = []
values = {}
column_names.each do |c|
params << "#{c} LIKE #{c.to_sym}"
values[c.to_sym] = search
end
params.join (' OR ')
where(params,values)
else
all
end
end
You've got the right idea, but invoking the .join method does not change the object on which it is called, it merely returns a string representation. You need to store the return in a variable, something like this: paramsStr = params.join(' OR '). Then simply pass paramsStr to the where clause.
Ultimately, that is what is causing your unidentified method % for Array .... error; this version of the where method is expecting the first parameter to be a string. Check out this documentation, the part about placeholder conditions.
Hope that helps.
I have a pretty common helper method in my application helper:
module ApplicationHelper
def quid(num)
number_to_currency (num / 100.0), unit: "£"
end
end
But my applications keeps throwing an 'undefined method `/' for nil:NilClass' error.
If I print number_to_currency (num) it doesn't have an issue, it's only when I try to put an operator like / * - + that it gives this error.
This was working fine until today, and it's only in the product index view that I get the error.
I'm not sure what's wrong, can anyone help explain? I'm stumped!
UPDATE:
Here's the product index view code:
<% #products.each do |product| %>
<h3><%= product.title %></h3>
<div id="link_text"><%= link_to "See more", product %></div>
<%= image_tag(product.avatar.url(:medium)) %><br/>
<%= product.description %><br />
<%= quid product.price_in_pence %>
<% end %>
price_in_pence is an integer, but turning 100.00 into 100 doesn't help, so I don't think that's the issue, I think it is because num is returning nil for some reason.
The error comes, because the num variable is nil in some cases. number_to_currency(nil) works, because the helper is implemented like this:
def number_to_currency(number, options = {})
return unless number
...
end
source: http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency
You see, a nil doesn't hurt here, because it will instantly return.
Your function, however, tries to execute nil / 100.0 first. As the error states, the / method is not defined on nil.
You probably need some special nil treatment, or hunt the cases when nil is given to your helper.
So basically in my partial I have the following line of code
...
<%= " active" if current_user."#{prop_row}" == "repeat-x" %>
...
So I tried to pass in the following variables "prop_id", "prop_row" using:
<%= render :partial => "users/image_props/repeat", :prop_id => "mbr", :prop_row => "main_background_repeat" %>
I get the error
/Users/codyjames408/rails/moz/app/views/users/image_props/_repeat.html.erb:4: syntax error, unexpected tSTRING_BEG
...= ( " active" if current_user."#{prop_row}" == "repeat-x" );...
...
^
I think the errors because its appending a string instead of the row method. But I am pulling my hair trying to figure how to work around this.
I would love to turn this into a big helper method or something! I just don't know how...
If prop_row is a string, containing the name of an attribute, you ucan do this:
<%= " active" if current_user.attributes[prop_row] == "repeat-x" %>
Or use this:
<%= " active" if current_user.send(prop_row.to_sym) == "repeat-x" %>
I want to print HAML at same line
%ul
- #categories.each do |c|
%li= c.name
(#{c.posts.count})
and I recive
<ul>
<li>vim</li>
(3)
<li>bash</li>
(1)
</ul>
How to get
<ul>
<li>vim(3)<li>
<li>bash(1)</li>
</ul>
Something like this should work:
%li= "#{c.name} (#{c.posts.count})"
I'm writing my first custom rails validation, and would like to tag the offending class with an html "error" class if they return false - I can't quite figure out how to do it. Relevant validation code below - any help appreciated.
(If it makes a difference, I'm using jQuery)
validates_each :shop do |record, attr, value|
shopvar = record.shops.map{ |s| s.email.downcase.strip }
if shopvar.count != shopvar.uniq.count
record.errors.add(attr, 'has the same email address entered more than once')
#record.errors[attr] << "You have entered this shop in the form twice"
end
end
So in your form you'd have something like this for an input field
<%= form.text_field :title %>
Since errors is a hash you could use the "include?" method like so...
errors.include?(:title)
This tells you that there's something wrong with this field. Now all you need to do is style it.
Whack on a ternary operator asi...
<% css_class = errors.include?(:title) ? "highlight_error_class" : "no_problem_class" %>
<%= form.text_field :title, :class => css_class %>
Done.