Rails validate error not displaying - ruby-on-rails

I have a Rails 3.2 app with this validate in the costquestion model:
validate :estimatesource
private
def estimatesource
if !(source_contractor? ^ source_other? ^ source_sqft_cost? ^ source_rs_means? ^ source_facilities_management?)
errors.add(:base, "Specify Estimate Source")
end
end
In the form, I have this:
<%= simple_form_for #costquestion, :html => {:class => 'form-horizontal'}, :validate => true do |f| %>
<%= f.error_notification %>
At the top of the page it displays "Some errors were found, please take a look:". I don't see the "Some errors were found, please take a look:" error.
How can I get it to display?
Thanks for the help!

SimpleForm highlights fields with errors that are visible. To display errors attached to base, you could use this:
<%= f.error :base %>
Or you can create your own helper to display list of errors at the top
<%- if #costquestion.errors.any? %>
<%- #costquestion.errors.full_messages.each do |message| %>
<%= message %>
<% end %>
<% end %>

Related

Trouble with Concatenation in Form

I am trying to generate dynamic CSS ids for javascript purposes, and the following code is giving me an error:
<%= form_for #item, html: { multipart: true } do |f| %>
<%= f.fields_for :item_images do |builder| %>
<%= image_tag builder.object.image.url(:large), :class => "cropbox", 'data-id' => builder.object.id %>
<% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
<%= builder.text_field attribute, :id => attribute + builder.object.id %>
<% end %>
<% end %>
<% end %>
I know I'm not concatenating attribute and builder.object.id properly, but I've tried everything with no luck. I get this error message:
undefined method `+' for :crop_x:Symbol
Appreciate any help, thanks!
What is your expected result?
Can you p out what '''attribute''' is within your expression?
Also, have you tried .concat instead of '+'?
Away from my desk, but my suggestion is something like:
Example:
attribute.concat(builder.object.id)
Maybe need to convert attribute.to_i or .to_s into a responsive type or create a different dynamic CSS id solution

Using observe_field on an field inside a fields_for

I'm trying to observe a field that get generated inside a fields_for loop.
The thing is that the id of that field is generated dynamically.
_form.html.erb
<% form_for #exp, :url => {:action => "update"} do |f| %>
<% f.fields_for:patterns do |builder| %>
<%= render 'pattern_fields', :f => builder %>
<% end %>
<% end %>
_pattern_fields.html.erb
Pattern: <%= f.select(:LC_PATTERN, [['stripes', 'stripes'],
['dots', 'dots'],
['lines', 'lines'],
],{ :prompt => "Please select"}
) %>
<%= observe_field("------", :frequency => 1,
:with => "'id='+value", :function => 'alert(value)')%>
My question is how do i get the id of the field inside the fields_for tag.
I finally got it. Found this solution on the internet...Hope it might be of help to someone else.
In your application_helper.rb, add the following functions:
def sanitized_object_name(object_name)
object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/,"_").sub(/_$/,"")
end
def sanitized_method_name(method_name)
method_name.sub(/\?$/, "")
end
def form_tag_id(object_name, method_name)
"#{sanitized_object_name(object_name.to_s)}_#{sanitized_method_name(method_name.to_s)}"
end
You can then view the id of the fields generated inside 'fields_for' by using the following code:
<%=form_tag_id(f.object_name, :LC_PATTERN) %>

Rails 3 Form File Handling Question

I have a Rails 3 application with a pretty standard multipart form that includes an image upload. However, it throws an error.
The form starts with:
<%= form_for(#object, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :photo %>
<%= f.file_field :photo %>
</div>
<div class="actions">
<%= f.submit 'Submit Object' %>
</div>
<% end %>
Then, I have this in the object.rb model:
def photo=(file_data)
unless file_data.blank
#file_data = file_data
self.extension = file_data.original_filename.split('.').last.downcase
end
end
This throws the following error:
undefined method `blank' for #ActionDispatch::Http::UploadedFile:0x37ecc78
Meanwhile, it also shows the image exists in the Request Parameters error page:
"photo"=>#ActionDispatch::Http::UploadedFile:0x37ecc78 #original_filename="Image.jpg",
Any help would be greatly appreciated!
The blank method doesn't exist. It's blank?, with the question mark.
unless file_data.blank?
...
Documentation

customized form_for tag in rails

I want to make a table within a form by making a new form_tag. The following code in ApplicationHelper fails:
module ApplicationHelper
class TabularFormBuilder < ActionView::Helpers::FormBuilder
# ... code to insert <tr> tags </tr>
end
def tabular_form_for(name, object = nil, options = nil, &proc)
concat("<table>", proc.binding)
form_for(name,
object,
(options||{}).merge(:builder => TabularFormBuilder),
&proc)
concat("</table>", proc.binding)
end
end
The view I use is:
<h1>New project</h1>
<% tabular_form_for :project, :builder => ApplicationHelper::TabularFormBuilder do |f| %>
<%= f.error_messages %>
<%= f.text_field :name %>
<%= f.text_area :description %>
<%= f.text_field :location %>
<%= f.submit 'Create' %>
<% end %>
The error I get is:
NoMethodError in Projects#new
Showing app/views/projects/new.html.erb where line #5 raised:
undefined method `errors' for {:builder=>ApplicationHelper::TabularFormBuilder}:Hash
Any ideas how to make this custom tag work?
Is this posted verbatim? Because your second block needs to be within the closing end tag for it to access the FormBuilder class right?
I found the following tutorial which might help:
http://ramblingsonrails.com/how-to-make-a-custom-form-builder-in-rails

What is the form_for syntax for nested resources?

I am trying to create a form for a nested resource. Here is my route:
map.resources :websites do |website|
website.resources :domains
end
Here are my attempts and the errors:
<% form_for(#domain, :url => website_domains_path(#website)) do | form | %>
<%= form.text_field :name %>
# ArgumentError: wrong number of arguments (1 for 0)
# form_helper.rb:290:in 'respond_to?'
# form_helper.rb:290:in 'apply_form_for_options!'
# form_helper.rb:277:in 'form_for'
<% form_for([#website, #domain]) do | form | %>
<%= form.text_field :name %>
# ArgumentError: wrong number of arguments (1 for 0)
# form_helper.rb:290:in 'respond_to?'
# form_helper.rb:290:in 'apply_form_for_options!'
# form_helper.rb:277:in 'form_for'
<% form_for(:domain, #domain, :url => website_domains_path(#website)) do | form | %>
<%= form.text_field :name %>
# ArgumentError: wrong number of arguments (1 for 0)
# wrapper.rb:14:in 'respond_to?'
# wrapper.rb:14:in 'wrap'
# active_record_helper.rb:174:in 'error_messages_for'
<% form_for(:domain, [#website, #domain]) do | form | %>
<%= form.text_field :name %>
# UndefinedMethodError 'name' for #<Array:0x40fa498>
I have confirmed both #website and #domain contain instances of the correct class.
The routes also generate correctly is used like this for example, so I dont think their is an issue with the route or url helpers.
<%= website_domains_path(1) %>
<%= website_data_source_path(1, 1) %>
Rails 2.3.5
You can use
<%= form_for [#website, #domain] %>
Where #domain belongs to #website
I've just tried:
<% form_for #note, :url => teams_person_notes_path(#person) do |f| %>
<%= f.text_field :note %>
<%= f.submit "Add" %>
<% end %>
and it works without any problem. My routes looks like this:
map.namespace :teams do |t|
t.resources :people do |p|
p.resources :notes
end
end
So it is the same as yours (only namespace is added, but it's not a case).
Of Course, your example form is for new object of #domain. It won't work for edit action, then you should have:
<% form_for #domain, :url => edit_website_domain_path(#website, #domain) do |form| %>
This was so obvious when it finally clicked, I had a field called respond_to. It was of course overriding an existing method.
Its a shame ActiveRecord does not warn when a database column will overwrite an already defined method...

Resources