No input found for varchar - ruby-on-rails

I am trying use nested models in my rails application but I have a little issue.
This is my view:
<%= simple_form_for #installation do |f| %>
<div class="field">
<%= f.label :x %><br>
<%= f.input :x %>
</div>
<%= f.simple_fields_for :address do |u| %>
<div class="field">
<%= u.label :street_address %><br>
<%= u.input_field :street_address %>
</div>
<% end %>
<% end %>
When I run, I receive this error <%= u.input_field :street_address %> -> "No input found for varchar", but when I change this peace of code to <%= u.input_field :street_address, :as => :string %> work. Why this happen?

The magic is simple form will automatic detect your data type and automatically pick a input control for it. For instance:
text => text_area
string => text field
boolean => checkbox
As the document described, there is not data type of varchar that simple form can understand autotically, so you need to specify the input type manually!
So you can use as: :string or as: :text to make it work!

Related

How do you ensure address form autofills correctly?

I have a form in Rails, part of which asks for an address:
<%= f.fields_for :postal_address do |ff| %>
<%= ff.text_field :apartment_number %>
<%= ff.text_field :building_name %>
<%= ff.text_field :building_number %>
<%= ff.text_field :street %>
<%= ff.text_field :town %>
<%= ff.text_field :postcode %>
<% end %>
However, when I click the little autofill thing in Safari (below) the form is filled in incorrectly.
This is obviously wrong:
Is there any way I can instruct the browser to autofill the fields with the expected data? Or is it solely up to the browser?
It should work better if you add autocomplete attribute to inputs. Browser tries to guess about field's purpose from its name, but not sure if it knows "Building number" and "Town"
<%= ff.text_field :town, autocomplete: 'address-level2' %> # 'city' should work too
You can check list of available values in the docs

how does fields_for know how many fields to create?

I guess I'm a little confused on how fields_for works. I have an action with this:
3.times { #submitted_quiz.submitted_answers.build }
if I write a form_for like this in the associated view:
<%= form_for(#submitted_quiz) do |f| %>
<%= f.hidden_field :quiz_id, :value => #quiz.id %>
<%= f.hidden_field :name, :value => #quiz.name %>
<%= f.fields_for (:submitted_answers) do |ff| %>
<%= ff.label :content, "Answer" %><br />
<%= ff.text_area :content, :rows => 3 %>
<% end %>
<%= f.submit %>
<% end %>
how does fields_for know to run three times?
Rails will simply check how many submitted_answers your submitted_quiz has and generate appropriate number of fields ( one for each answer obviously ).

properly storing date info using form_for

I have a form that allows a user to create an event. One of the fields that the user fills out is date. Currently this is the code for the date filed:
<%= u.label :date %> <%= u.text_field :date, :placeholder => 'mm/dd/yyyy' %>
For some reason, if the user types in 01/02/2012 it stores as dd/mm/yyyy, thinking the event is on Feb 1 instead of Jan 2nd. I've already configured my initializer files to display date/datetime the way I would like it to (as recommended on several posts here) but this is still an issue
Update - here is my full form:
<%= form_for(#party_profile) do |u|%>
<p>
<%= u.label :name %><%= u.text_field :name %>
</p>
<p>
<%= u.label :location %><%= u.text_field :location %>
</p>
<p>
<%= u.text_field :date, :placeholder => 'dd/mm/yyyy' %>
</p>
<p>
<%= u.label :password %> <%= u.text_field :password %>
</p>
<%= u.submit "Let's Party!", :class => "btn btn-primary" %>
<% end %>
I have tried replacing u.text_field with select_date as suggested below, but then I get an error:
undefined method `select_date' for #<ActionView::Helpers::FormBuilder:0x007f9e5e8c69d8>
<%= u.label :date %> <%= u.date_select(:date, :order => [:month, :day, :year]) %>
is the best I could come up - still not formatted exactly how I'd like
I use that for my dates:
<%= u.label :date %>
<%= u.date_field :date %>
Hope it helps =)
Consider using select_date, rather than a simple text field where the user can type in anything.
<%= u.select_date(:date, :order => [:month, :day, :year]) %>
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_date

Rails 3.1 - Displaying form values on Edit?

I feel like this should be really really simple, but I'm completely stuck!
In a form I might have a field like:
<%= f.text_field :name, :class => "text" %>
On edit, this pulls back in the value submitted on create, that's fine. But I want to prevent certain fields from being edited. I know I can disable the field or hide it, but I'd like to be able to pull in the values to display and use in other ways. How do I access them?
In this case I've tried things like:
<%= f.track.name %>
<%= track.name %>
<%= #track.name %>
But none of the above work!
Any ideas folks?
EDIT: (I'm using nested forms for this)
<%= form_for(#release, :html => { :multipart => true }) do |f| %>
<h3>Upload Tracks for <%= #release.title %></h3>
<%= f.fields_for :tracks do |builder| %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>
<%= f.submit "Upload Tracks", :class => "submit" %>
<% end %>
And the upload_track_fields that are rendered:
<%= f.text_field :position, :class => "text" %>
<%= f.text_field :name, :class => "text" %>
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
<%= f.hidden_field :primary_genre_id %>
<%= f.hidden_field :secondary_genre_id %>
<%= f.hidden_field :alt_primary_genre %>
<%= f.hidden_field :alt_secondary_genre %>
<%= f.hidden_field :asset_tier %>
<%= f.hidden_field :preview_start %>
<%= f.hidden_field :parental_advisory %>
<%= f.hidden_field :available_separately %>
<%= f.hidden_field :_destroy %>
I've hidden most of the fields to prevent editing, but still need to see some of the fields so they're left as text fields. I tried to disable them, but that stops any changes (specifically the file upload) working.
In short, i'd prefer to display most of the above as text rather than form fields.
In the main form:
<% index = 0 %>
<% f.fields_for :tracks do |builder| %>
<%= #release.tracks[index].name %>
<%= render 'upload_track_fields', :f => builder %>
<% index += 1 %>
<% end %>
In the nested form:
<%= f.text_field :position, :class => "text" %>
# Notice that there's no "name" attribute
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
What I did in the first snippet is dirty, but I never used fields_for so I don't know how to get the actual index. I looked in Google, but I didn't find a solution so far.
I can't try it right now, I'll do it when I'll be home.
I suggest using this while finding a way to get the index.
Good luck!
As those who have commented said, I'd assume the <%= #track.name %> should work, if you have #track = #release.track (for instance) in your edit method in the controller.
Instead of keeping track of the index you can access the associated objects through builder, it's actually a track
<% f.fields_for :tracks do |builder| %>
<%= builder.object.name %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>

Fill rails form conditionally

I'm making a simplistic message board with tags. The message#index view displays a list of all messages. The tag#show view shows messages of a specified tag. On the message#index view, there is a form (partial) that requires the user to write a message and to tag it. On the tag#show view, I'd like to use the same form partial but to have the view's tag automatically filled into the form. In the show action of the tags controller, the name of the tag is #title.
The form partial looks like this:
<% form_for :message, :url => { :action => "create" }, :html => { :id => 'form' } do |f| %>
<%= f.error_messages %>
<%= f.label :tag, "tag<p2>( separate tags with a comma )</p2>" %>
<%= f.text_field :tag_list %>
<%= f.label :name, "name<p2>( optional )</p2>" %>
<%= f.text_field :name %>
<%= f.label :email, "email<p2>( optional )</p2>" %>
<%= f.text_field :email %>
<%= f.label :title, "message" %>
<%= f.text_area :content %>
<%= f.submit 'submit' %>
<% end %>
How do I auto fill the tag_list text field with the #title value? #title is a string. I appreciate any help you can offer. Thank you.
<%= f.text_field :tag_list, :value => #title %>

Resources