Rails: Change displayed error column names - ruby-on-rails-3

I've tried following the posts found here: Rails 3 Change Error Message and here: ActiveRecord validates... custom field name but neither has worked for me and my database field names still are displayed.
For example I get: Name is too short (minimum is 1 characters)
Any thoughts/Is there a preferred way to troubleshoot this? Thanks.
Here is what I have using the first linked article in my en locale:
en:
activerecord:
models:
account: "Account"
attributes:
order:
name: "Business Name"
Here is a bit of my account model:
validates_presence_of :name
validates_length_of :name, :minimum => 1, :maximum => 100
attr_accessible :name, :admin_attributes, :image
After a failed save attempt on the account, here is the code to display errors from my view:
<% if #account.errors.any? %>
<div class="errorExplanation">
<h2>Errors encountered with your account information:</h2>
<ul>
<% #account.errors.full_messages.each do |m| %>
<li><%= m %></li>
<% end %>
</ul>
</div>
<% end %>

I know this is quite old, but I found this question before finally just turning to the rails guides, which got my code working.
It looks like you want to change the attribute name for Account, but are using Order:
en:
activerecord:
models:
account: "Account"
attributes:
order: # Right here, this should be account
name: "Business Name"
I don't believe you need the models: account: "Account" either. This is to tell rails if you want to call the model something different, but you don't, so you can remove it. Attributes then takes the model for what you want to change, and then the attribute.
en:
activerecord:
attributes:
account:
name: "Business name"
I think what may have thrown you off is that it almost looks like attributes belongs to models, but it's actually on a different line. Hope this helps, as late as it is!

Related

belongs_to association and formtastic

I have a model Product which has a belongs_to association with another model Type. In the product's form, I'm using formtastic to display a select tag with all the types available in the database, like this:
<%= f.input :type %>
The select is showing up OK, but each option of it is an object instance of the Type model as a string, for example:
#<Type:0x00eff180c85c8>
Instead of that, I'd like to display the 'title' attribute of it, like:
Electronic
Domestic
...
Any ideas?
Try the member_label option, it sounds like what you want to do:
<%= f.input :type, :member_label => :title %>
The documentation has more examples.
Simply add this in your model
def name
return self.title
end

rails 3 select control from nested models

I have 2 models user and team. a team can have many users each users belong to a team.
team_id is a column in my user table as a fk.
I want to have a select drop down on my create user form that has all my teams team_id in it.
This code does what I want BUT
<%= f.fields_for :teams do |builder| %>
<p>
<%= f.label :team_id, "Select Team For User" %>
<%= builder.collection_select :team_initials, Team.all, :id, :team_initials %>
</p>
<% end %>
it does not try to add the id value to a user. here is the error
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"xxxxx",
"user"=>{"name"=>"brand new",
"email"=>"test1#test.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"teams"=>{"team_initials"=>"27"}},
"commit"=>"Create my account"}
It complains that it cant add to the team model.
All I want to do is grab the ID of all existing teams for my create user form so when you crate a user you can select that users team.
The reason I have team_initials above is I was trying to use a more user friendly field for select but still user team Id as the value.
I think you're on the right track. The fields_for mechanism is for setting values in the associated model. I think all you're trying to do is make the association.
So you can lose the fields_for line and then this section:
builder.collection_select :team_initials, Team.all, :id, :team_initials
Should be:
f.collection_select :team_id, Team.all, :id, :team_initials
I'm looking here for my info on collection_select since I don't know it off the top of my head:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
I think this will change this parameter from "teams"=>{"team_initials"=>"27"}}, to "team_id"=>"27",
I hope that fixes it.

Accessing attributes from different associated models rails 3

I am looking to get a better understanding of Active Model/Record relationships and how to call attributes dependent upon where the attributes are located (models) and where I am calling them. So for example I can access the attribute dish_name from within the recipe controller like so
def all_recipes
#recipes = Recipe.all
end
In the view
<% #recipes.each do |r| %>
<%= r.dish_name %>
<% end %>
Now say i want to access a recipe attribute from within my controller called worldrecipes and i have just written a method that returns all recipes with the same country. a country has many recipes as a relation
So my method is
def self.top_countries
joins(:recipes).
select('countries.*, count(*) AS recipes_count').
group('countries.id').
order('recipes_count DESC')
end
My controller
#worldrecipes = Country.where(:name => params[:name])
and view
<% #worldrecipes.each do |r| %>
<%= r.name %>
<% end %>
so accessing the country name attribute is easy as its in the country model and thats where my query results are being returned from (I think)...My question is how do i access the dish_name attribute from my recipe model to that links to the country name
Hope that makes sense, does anyone have a guide on how to work this out or some golden rules for this
Thank you
I think what you need is:
#country=Country.where(:name=>params[:name]).first
#worldrecipes=#country.recipes
And in the view:
<% #worldrecipes.each do |r| %>
<%= r.dish_name %>
<% end %>
This would print the dish names of the recipes of the country with name provided by params[:name]
EDIT:
Ok Let me clear this up for you :)
Your model relationship is setup such that each country has many recipes. i.e a country has many recipes.
So you have,
has_many :recipes
in country.rb and
belongs_to :country
in recipe.rb
Now when you want to access all the recipes belonging to a country, what you do is, you call country_record.recipes (country_record being the object of the country record you need).
And when you call,
Country.where(:name=>params[:name])
What you actually get is the active record object representing the COUNTRY itself and not the recipes of the country and that is why Italy was printed.
Hope this helped you.
For starters you want to make sure you have the association setup in your models:
country.rb
class Country < ActiveRecord::Base
has_many :recipes
end
recipe.rb
class Recipe < ActiveRecord::Base
belongs_to :country
end
If you haven't already done so, add a foreign_key attribute to your recipe model by running the following migration:
rails g migration add_country_id_to_recipe country_id:integer
Now that your associations are in place you can easily query for a countries respective recipes. In your controller:
#worldrecipes = Country.where(:name => params[:name])
Then in your view:
<% #worldrecipes.each do |c| %>
<% c.recipes.each do |r| %>
<%= r.dish_name %>
<% end %>
<% end %>
In regards to 'golden rules' I highly recommend you check out Association Basics. This is the go-to place for an overview of what you can do with associations.

Rails 3 Devise adding field to registartion form

I want to add new field to registration form.
For this:
1. I created field named user_name in my database
2. I changed my model
attr_accessible :email, :password, :password_confirmation, :remember_me, :user_name
3. I changed the view
<%= f.label :user_name %>
<%= f.text_field :user_name %>
But I got the error: undefined method 'user_name' for #User:0x1ff0e30
Could anyone help me?
How did you create the field? It looks like the model isn't recognizing that the field is there which could be one of two things.
First, you've not run the migration to add the field to the table in the database. This is the most common mistake of people when this error is encountered.
Second, you did add the field, but you added it to the wrong database. Less common, but still a potential possibility.

Rails 3: trying to follow railscast #17 to add genres to my posts

I've followed along exactly like the Railscast describes but using "genre" instead of "category," but whenever I make a post the genre_id shows as NULL and I'm getting a WARNING: can't mass assign attribute genre_ids
I only had genre_id in my post model attr_accessible because that's the name of the attribute, however when I change it to genre_ids. I get the error
ActiveRecord::StatementInvalid in PostsController#create Could not find table 'genres_posts'
post form
<% for genre in Genre.find(:all) %>
<div>
<%= check_box_tag "post[genre_ids][]", genre.id, #post.genre.include?(genre) %>
<%= genre.name %>
</div>
<% end %>
post model
class Post < ActiveRecord::Base
has_and_belongs_to_many :genres
end
I don't have an answer, but here are the things you should check:
In your code, the line <%= check_box_tag "post[genre_ids][]", genre.id, #post.genre.include?(genre) %> should include genres, not genre:
<%= check_box_tag "post[genre_ids][]", genre.id, #post.genres.include?(genre) %>
What Ryan Bates (I love his webcast) doesn't tell is that you need changes on your database so that your models may be stored. The reason for that is, that on a relational database, a m:n relation is normally realized by a separate table that just stores that relation only. Have a look at the "Rails Guides: Has and belongs to many associations".
So you have to generate a standalone migration (see the Rails Guide for that) that creates that table that hold both ids to posts and genres in one table. The table should be named therefore genres_posts, and include the columns genre_id and post_id.
So depending on what you have done before, you may follow all the steps to reach a working example application. Good luck!