Rails form tags - ruby-on-rails

I have a form to allow the user to upload an image file, but i currently have it in text format, so they enter the file name of the image, but i would like to have it so they click a select button to upload the file rather than typing it in, and then when the file is selected from their hard drive, it will show in the textbox next to the button, thanks.
<%= form_for(#question) do |f| %>
<p>
<%= f.label :question %><br/>
<%= f.text_field :question, autofocus: true %>
</p>
<p>
<%= f.label :description %><br/>
<%= f.text_area :description, cols: 40, rows: 7 %>
</p>
<p>
<%= f.label :posted_by %><br/>
<%= f.text_field :posted_by, size: 5 %>
</p>
<p>
<%= f.label :image_file_name %><br/>
<%= f.text_field :image_file_name, size: 30, placeholder: 'GIF, JPG, or PNG file' %>
</p>
<p>
<%= f.label :posted_at %><br/>
<%= f.date_select :posted_at %>
</p>
<p>
<%= f.submit %>
<%= link_to('Cancel', questions_path) %>
</p>
<% end %>

You can use file_field instead of text_field which allows to upload a file.
<%= f.file_field :image_file_name,accept: 'image/png,image/gif,image/jpeg',:multiple => true %>
:multiple - If set to true, in most updated browsers the user will be allowed to select multiple files.
:accept - If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations.
For more details, see this API

An answer can be found here: https://stackoverflow.com/a/10526674/786709
(source: http://guides.rubyonrails.org/form_helpers.html#uploading-files)

Try to use the file_field instead of text_field.
<%= f.text_field :image_file_name, accept: 'image/png,image/gif,image/jpeg' %>
See more: FormHelper.html#method-i-file_field

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 to Set Up A Form Submission System In Rails That Handles Many Different Fields

I'm trying to set up a page in Ruby on Rails which accepts a lot of information from the user via forms then saves it (& also allows fields to be editable). I've set up forms before but only for a single title/content form, what I want will be a page that will have a form for:
Personal Details: Name, Age, Contact Details
Company Details: Name, Age, Contact Details
I'm not sure at all how to do this, if anyone could link me to a guide that explains how to do this or can explain it to me it would be much appreciated.
It sounds like you have "Company" in a separate model. If so, you need something similar to this code. Your controller will need to initiate both #user and #company.
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: #user %>
<h3>Yourself</h3>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
<%= f.label :age%>
<%= f.text_field :age, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<h3>Your Company</h3>
<%= fields_for(#company) do |c| %>
<%= c.label :name %>
<%= c.text_field :name, class: 'form-control' %>
<%= c.label :age%>
<%= c.text_field :age, class: 'form-control' %>
<%= c.label :email %>
<%= c.email_field :email, class: 'form-control' %>
<% end %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
For such a task you can just use scaffold
rails g scaffold User age name street zip company_name etc..
Type this into your console
This will create a complete scaffold for you. You can create and edit your form.
It's a good way to start if you have no other idea.

Carrierwave Rails with Edit Form and file_field (No file selected)

When my edit view loads, all my fields are populated with the record detail, but the file_field shows "No file Selected" next to the browse button. I would prefer that is show the record file name instead. Why does it not show that a file that was uploaded?
Edit View
<%= form_for #document, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :file, "File" %><br />
<%= f.file_field :file %><br />
<%= f.hidden_field :file_cache %><br />
<%= f.label :name, "name" %><br />
<%= f.text_field :name %><br />
<%= f.label :memo, "memo" %><br />
<%= f.text_area :memo %>
</p>
<%= f.hidden_field :folder_id %>
<p><%= f.submit "Upload" %></p>
<% end %>
I think a good counterquestion is "why should show an uploaded file by default?".
But basically is because security reasons and none of the browsers (yep, nothing to do with rails) is going to allow that, so you must to indicate manually to the user about the uploaded files.

Rails creating multiple entries at once

I am trying to figure out the cleanest way to create multiple DB entries at once (or if it's even a good idea).
I have an Event model, which has many Bands and many Venues, through the event_bands and event_venues models.
Right now, the user first creates the event, then is redirected to the form to create the event_band and event_venue relationships. I would like to streamline all of this into one form if possible, like this (bottom lines are most relevant):
<%= form_for #event do |f| %>
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :contact %>
<%= f.text_field :contact %><br />
<%= f.label :price %>
<%= f.text_field :price %><br />
<%= f.label :info %>
<%= f.text_area :info %><br />
<%= f.label :date %>
<%= f.date_select :date, start_year: 2014 %><br />
<%= f.label :time %>
<%= f.text_field :time, placeholder: "7:40pm" %><br />
<%= f.label :band %>
<%= select(:event_band, :band_id, options_for_select(Band.order(name: :asc).collect { |b| [b.name, b.id]} )) %><br />
<%= f.label :venue %>
<%= select(:event_venue, :venue_id, options_for_select(Venue.order(name: :asc).collect { |v| [v.name, v.id]} )) %><br />
<%= f.submit %>
<% end %>
Because the event_band and event_model require event.id, I guess I would have to use an after_create callback to build and save them. The issue I'm having conceptualizing this is that I want to post those extra :band_id and :venue_id params through the form for the event.
Is there a cleaner way to handle posting params for multiple models through one form and then using them in a callback or am I headed in the wrong direction?
You can use fields_for : see doc

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

Resources