How do you set the model name in config/locales/en.yml for scaffolding like <%= f.submit %>?
en:
hello: "Hello world"
activerecord:
models:
blog_post: "Post"
Related
I have tweaked the bootstrap wrapper so that now I display all inline errors as a block above the simple_form field.
The form is a registration Devise form, specifically from the rails-prelaunch-signup composer app. How can I include the name of the field in the error message?
At the moment I am getting "isn't valid" or "can't be blank", however I would like something like "Email can't be blank".
Simpleform refers to rails localization if no error message is set in the model. So, if you want to add the attributes name to each error message, you could add something like this in your locale-file:
en:
errors:
messages:
blank: "%{attribute} can't be blank"
invalid: "%{attribute} isn't valid"
Where %{attribute} is the placeholder where the fields name will be inserted.
You can set the error message in the model:
validates :email, presence: { error_message: "Email can't be blank" }
To add the attribute name on every error message, you can use the full_error helper instead of the classic error helper
Directly in your form
<%= simple_form_for #user do |f| %>
<%= f.label :username %>
<%= f.input_field :username %>
<%= f.full_error :username %>
<%= f.submit 'Save' %>
<% end %>
Or in your custom wrappers
# config/initializers/simple_form.rb
SimpleForm.setup do |config|
# ...
config.wrappers :vertical_form do |b|
# ...
b.use :label
b.use :input
b.use :full_error
end
end
I have the following code in my Rails 3 application, it's supposed to be displaying a select box with each asset_type record:
assets_helper
def asset_type_all_select_options
asset_type.all.map{ |asset_type| [asset_type.name, asset_type.id] }
end
_form.html.erb (Asset)
<%= f.select :asset_type_id, asset_type_all_select_options, :class => "input-text", :prompt => '--Select-----' %>
and here are my models:
asset.rb
belongs_to :asset_type
asset_type.rb
has_many :assets
Using the above code I get the following error:
undefined local variable or method `asset_type' for #<#<Class:0x007f87a9f7bdf8>:0x007f87a9f77d48>
Am I doing something wrong? Will this method not work with double barrel model names? Any pointers would be appreciated!
The variable asset_type in your assets_helper file is not defined. You would need to pass it in to the helper method
def asset_type_all_select_options(asset_type)
# ...
end
Or use an instance variable that you define in the controller (e.g. #asset_type).
However, you can simplify this by using the #collection_select form helper.
_form.html.erb (Asset)
<%= f.collection_select :asset_type_id, AssetType.all, :id, :name, { prompt: '--Select-----' }, class: 'input-text' %>
Take a look at the API for #collection_select for details.
I have a simple form which uses jquery-tokeninput as shown in the code below (http://loopj.com/jquery-tokeninput/, https://github.com/loopj/jquery-tokeninput). Can I convert the form to use formtastic and keep the data attribute working? what should I do to replace data: {load: #product.tags} ?
<%= f.text_field :tag_tokens, data: {load: #product.tags} %>
Something like this, using the input_html Hash:
<%= f.input :tag_tokens, input_html: { data: { load: #product.tags } } %>
I want to include country selection select box in my Rails web form. How is it possible in Ruby on Rails? My form is like this
<%= form_for :User, :url => {:action => :create } do |f| %>
<div class="field">
<%= f.label :select_countries %><br />
<%= f.select(:countries, country_list) %>
</div>
<%= f.submit "Create" %>
<% end %>
What can I include in place of country_list?
My opinion is to seed the countries list in database.
Create a model country with field 'name'.
In app/models/country.rb
attr_accessible :name
Load the list of country from the YAML file and seed into the database.
In config/country.yml
-
name: India
name: Pakistan
name: Cuba
#add required country name
In db/seed.rb
COUNTRIES = YAML.load_file(Rails.root.join('config/country.yml'))
COUNTRIES.each do |country|
Country.create(country)
end
Run
rake db:seed
Add country_id field in your user model(Write a migration to add field).
In app/models/user.rb
class User < ActiveRecord::Base
#associate user with country
belongs_to :country
end
In your new user form add the below code.
<%= f.select :country_id, Country.all.collect { |country| [country.name, country.id] },
{ :prompt => "Select Country" } %>
<%= f.country_select :country, ["United States"] %>
It really works for country_select gem
Rails used to provide a country select feature in the past. The feature has been extracted out from core and it's now packaged into the country_select plugin.
Use the plugin to enable the feature.
I'm using Mongoid/MongoDB with Rails and am trying to get a many-to-many relationship working. Basically books and categories where books can be in multiple categories. I keep getting an error:
undefined method `metadata' for "4e6aaec8ffb1900c19000002":String
when trying to add a new book and place it in categories. The following is what I'm using for the models, form, create method and what the server is reporting.
It looks like it is trying to update book_ids and the cat_ids, but it's not getting anything for the cat_ids. I've been trying lots of different things, but am not sure how to make this work.
The book model
class Book
include Mongoid::Document
field :title, :type => String
field :description, :type => String
has_and_belongs_to_many :cats
end
The cat model (categories)
class Cat
include Mongoid::Document
field :name, :type => String
field :description, :type => String
has_and_belongs_to_many :books
end
This is from the form that generates the category select and allows multiple selections:
<div class="field">
<%= label_tag "Cats" %><br />
<%= f.collection_select :cats, Cat.all, :id, :name, {}, :multiple => true %>
</div>
The create method in the books controller:
def create
#book = Book.new(params[:book])
redirect_to(#book, :notice => 'Book was successfully created.')
end
From the server when submitting the form:
Started POST "/books" for 127.0.0.1 at Fri Sep 09 17:30:37 -0700 2011
Processing by BooksController#create as HTML
Parameters: {"commit"=>"Create Book", "authenticity_token"=>"+OAIJM3NRPrUv0u1yfDEkkE2gvPQ7n0P6zPU9ZtqXlk=",
"utf8"=>"✓", "book"=>{"title"=>"The Golf & Tennis Book",
"cats"=>["4e6aaec8ffb1900c19000002", "4e6aaee8ffb1900c19000006"],
"description"=>"Both golf and tennis in this book, so it's in both categories."}}
MONGODB blog_development['system.namespaces'].find({})
MONGODB blog_development['cats'].update({:_id=>{"$in"=>[]}}, {"$pull"=>{"book_ids"=>BSON::ObjectId('4e6aafadffb1900c1900000b')}})
MONGODB blog_development['system.namespaces'].find({})
MONGODB blog_development['books'].update({"_id"=>BSON::ObjectId('4e6aafadffb1900c1900000b')}, {"$set"=>{"cat_ids"=>[]}})
Completed 500 Internal Server Error in 24ms
NoMethodError (undefined method `metadata' for "4e6aaec8ffb1900c19000002":String):
app/controllers/books_controller.rb:46:in `new'
app/controllers/books_controller.rb:46:in `create'
This is invalid
book.update_attributes({:cats => [ cat.id ]})
should be this
book.update_attributes({:cat_ids => [ cat.id ]})
or
book.update_attributes({:cats => [ cat ]})