Ruby on Rails default option value - ruby-on-rails-3

I am using an array to store my values for a f.select dropdown in my form
<%= f.label :country_of_origin %>
<%= f.select :country_of_origin, [['Wales'],['Scotland'],['England']] %>
Rather than the default value being Wales I would like it to say "Please Select" (obviously don’t want to be able to post the value "please select). Can anyone point me in the right direction please?

<%= f.select("model_name", "model_id", [['Wales'],['Scotland'],['England']], {:include_blank => 'Please Select..'}) %>

Related

How to create formtastic form with multiple language support (Internationalization)

I need to set up two languages for a single form using internationalization.
This is the proposed form page
<%= semantic_form_for #detail do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :dob %>
<%= f.input :gender, :as => :radio, :label => "Gender", :collection => [["Male", 'male'], ["Female", 'female']] %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
This is wk.yml file
wk:
formtastic:
labels:
detail:
dob: "Data of birtha"
name: "Youre Nama"
gender: "Gendera""
This is en.yml file
en:
formtastic:
labels:
detail:
dob: "Date of Birth"
name: "Your Name"
gender: "gender"
I have added Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true in formtastic.rb initializer.
I was successful in using en.yml.
I need to switch from 'en' to 'wk' and vise-verse.
How to achieve it via drop down box?
That's not something related to formstatic, but rather to your rails code.
All you have to do in order to switch to wk is
http://guides.rubyonrails.org/i18n.html
I18n.locale = :wk
In order to let your client to choose his language for the website, probably a good place to start is on this link: http://guides.rubyonrails.org/i18n.html
If all you want is to update the form (and not the rest of the website) in different languages on a user action, like selecting the language from a select box, you can use an ajax listener on the select box, that could require something like "http://www.yourwebsite.com/:locale/form/new" which will answer with an ajax action and will replace your form with the selected language (so on :locale you will pass the value of your select box for the language).

Rails 3 f.select not showing current value

I have the following:
<%= f.select :phase_names, options_for_select(["RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"]), :class => 'inputboxes' %>
It is storing it in the database, but in the edit view it does not show the stored value. How do I show it?
This:
options_for_select(your_array_list, your_selected_value)

Update model with response from an API after submitting form that uses nested attributes

Forgive me if this has been answered. I spent a few hours searching for the answer to this both here and on Google.
I'm just getting started with Ruby on Rails. I'm trying to update a model with data from an API response after saving the data originally submitted via the form to two different models.
So basically I have a User model and a Character model. The sign up form collects data on the user and their character. In order to make an API request I have to at least get a few details about the character to pass in as part of the requested attributes (i.e. name, realm).
So I'm using a form_for with nested attributes:
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Password Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.fields_for :characters do |builder| %>
<%= builder.label :realm %>
<%= builder.text_field :realm %>
<%= builder.label :name %>
<%= builder.text_field :name %>
<% end %>
<%= f.submit "Sign Up" %>
<% end %>
This successfully submits all of the user info submitted to the User model and the realm and name submitted to the Character model. No problems there. However, I have several more fields that I want t fill in for the Character records just created from the API that provides all of the character info. What is the most efficient way to do this so that after the form submits, it updates the record (or adds data prior to save the record) with the data coming back from the API.
I don't need help on the specific fields and data returned. I have that all working, I'm just looking for a way to make the API request and complete the record with all data immediately after the form submits.
Suggest to use callback (:after_create) .
Depending upon the API processing you can choose between them .
:after_save
:before_save
:before_create
With any callback just get the data from your API and fill other character info's.
Hope that helps . Good luck .

Rails 3 ActiveRecord::UnknownAttributeError "unknown attribute: _destroy" for nested records

Let's say I have a schema in which an apple crate contains zero or more apples. While editing the apple crate in a form, I want to list the apples and provide a checkbox next to each apple, for deleting it when the form is submitted.
There is nothing going wrong that I can see. In my model I say
class AppleCrate < ActiveRecord::Base
has_many :apples
accepts_nested_attributes_for :apples, :allow_destroy => true
...
end
I have the form working, so far as I can tell. The checkboxes appear in the form html and when the form is processed by the controller each apple in the list has an attribute called "_destroy" which is set to either "1" or "0" depending on whether or not I checked the box before submitting.
According to the Rails API, when I set _destroy to 1 and save, the apple should be deleted. But when I submit the form I get
ActiveRecord::UnknownAttributeError in AppleCrateController#update
unknown attribute: _destroy
...
"apple_crate"=>{"id"=>"10101", "apples"=>{"1"=>{"id"=>"1",
"variety"=>"granny smith",
"apple_crate_id"=>"10101",
"_destroy"=>"1"},
"2"=>{"id"=>"2",
"variety"=>"fuji",
"apple_crate_id"=>"10101",
"_destroy"=>"1"},
"3"=>{"id"=>"3",
"variety"=>"macintosh",
"apple_crate_id"=>"10101",
"_destroy"=>"0"},
...
and so on.
I must be missing something obvious but after several days of futzing around I can't figure it out. I can successfully do everything else -- update, edit, index, etc -- so long as I leave out the :_destroy attribute. Any ideas?
(For what it's worth, I'm running rails 3.2.2 on Windows.)
Updated:
This is what I'm looking at in the documentation. (See the subsection "One-to-many".)
Updated:
As requested in comments, here is the view:
<%= form_for #apple_crate do |f| %>
<% #apples = #apple_crate.apples %>
<% #apples.each do |apple| %>
<%= fields_for "apples[]", apple do |apple_fields| %>
<%= apple_fields.text_field :variety %>
<%= apple_fields.hidden_field :apple_crate_id %>
<%= apple_fields.hidden_field :id %>
<%= apple_fields.check_box :_destroy %>
<% end %>
<% end %>
<%= f.submit "Save" %>
<% end %>
You should generate nested forms and forms with rails helpers, don't do it by your hands. So I think that's where your error at.
Try:
<%= form_for #apple_crate do |f| %>
<%= f.fields_for :apples do |apple_fields| %>
<%= apple_fields.text_field :variety %>
<%= apple_fields.hidden_field :apple_crate_id %>
<%= apple_fields.hidden_field :id %>
<%= apple_fields.check_box :_destroy %>
<% end %>
<% end %>
something like this, did not check if it's correct, but idea should be clear enough

I have a two form with a single submit button. Then how to i need to save the values of those form values?

form with the single submit. How to store that values in the data base
<%= form_for (#movie) do |f| %>
//Movie
<%= f.label :moviename,"Movie Name:"%>
<%= f.text_field :moviename%>
//Releases
<%= f.fields_for :release do |release_fields| %>
<%= release_fields.label :theatre,"Theatre :"%>
<%= release_fields.text_field:theatre%>
<%= release_fields.label :city,"City :"%>
<%= release_fields.text_field:city%>
<%= release_fields.label :releasedate,"Release Date :"%>
<%= release_fields.text_field:releasedate%>
//Submit:
<%= f.submit "Save The Movie"%>
<% end %>
<% end %>
this is the form with the single submit button.There an error it showing the unknown attribute release can any on help with is issue Please
fields_for is expecting to access #movie.release, does that exist?