Newbie question: Select with Model

Hi all,

I’ve a entry model and I’ve a entry_type model. How do I to add a select
to
my view to edit the entry type and focus the current type.

Regards,


TSU. Amador Cuenca

I tried to do it by myself,

I’ve a model named Entry with a field named type_id (added via
migration)
and a model named EntryType with a field named description.

class Entry < ActiveRecord::Base

belongs_to :entry_type

end

class EntryType < ActiveRecord::Base

has_many :entries

end

I added the following code line to my /_form.erb:

<%= f.select(‘entry’, ‘type_id’, EntryType.all.collect {|t|
[t.description,

t.id]}, {:include_blanks => ‘None’}) %>

but when I try to load the page I get this exception:

NoMethodError in Entries#new

Showing
I:/Labs/RailsProjects/sisingresos/app/views/entries/_form.html.erb
where
line #18 raised:

undefined method `merge’ for [[“Ordinario”, 1], [“Especial”, 2],
[“Concejo Comunal”, 3]]:Array

Extracted source (around line #18):

15:
16:


17: <%= f.label t(‘entries.fields.type’) %>

18: <%= f.select(‘entry’, ‘type_id’, EntryType.all.collect {|t|
[t.description, t.id]}, {:include_blanks => ‘None’}) %>
19:

20:

21: <%= f.label t(‘entries.fields.generalentrydate’) %>

Trace of template inclusion: app/views/entries/new.html.erb

Rails.root: I:/Labs/RailsProjects/sisingresos
Application Trace http://localhost:3000/entries/new# | Framework
Tracehttp://localhost:3000/entries/new#
| Full Trace http://localhost:3000/entries/new#

app/views/entries/_form.html.erb:18:in block in _app_views_entries__form_html_erb__326164559_25362852_503356076' app/views/entries/_form.html.erb:1:in_app_views_entries__form_html_erb__326164559_25362852_503356076’
app/views/entries/new.html.erb:3:in
_app_views_entries_new_html_erb___750658673_25375572__1012098428' app/controllers/entries_controller.rb:23:innew’

Regards,

TSU. Amador Cuenca

I don’t know if this is the problem, but RAILS_ENV has been deprecated
in favor of Rails.env

Solved,

I do this:
<%= select(:entry, :type_id, EntryType.find(:all).collect {|t|
[t.description, t.id]}, {:include_blanks => true}) %>

if I put: f.select, It throws a exception…

Regards,

TSU. Amador Cuenca

Hey there Amador,

I must confess, I am not a rails guru here but, according to the
documentation I have, the part that reads

<%= f.select(‘entry’, ‘type_id’, EntryType.all.collect {|t|
[t.description,
t.id]}, {:include_blanks => ‘None’}) %>

should end with a *{ :include_blanks => false } *not with a ‘None’
string
value.

The other thing here is, if an annotation of your models look something
like
this:

Table name: entities

id :integer not null, primary key

type_id :integer

.
.
.

created_at :datetime

updated_at :datetime

Table name: entity_types

id :integer not null, primary key

type :string(255)

.
.
.

created_at :datetime

updated_at :datetime

Then in my understanding rails make the relationship between the two
models
if you have a field name containing the name of the table you want link
to
followed by an underscore and the name of the primary key of the
referred
table: *i.e. enty model needs an entity_type_id field not type_id. *

Well I guess that all I can see from what you have provided but like I
said
I am not an expert and I stand to be corrected. Hope this helps

Regards,
Tsega

On 1 February 2011 20:50, Tsega [email protected] wrote:

.

created_at :datetime

updated_at :datetime

Table name: entity_types

id :integer not null, primary key

type :string(255)

Don’t use a field called type unless you are using STI. ‘type’ is a
reserved word in Rails.

Colin

On 1 February 2011 20:20, Amador Antonio C. [email protected]
wrote:

I tried to do it by myself,
I’ve a model named Entry with a field named type_id (added via migration)
and a model named EntryType with a field named description.

class Entry < ActiveRecord::Base

belongs_to :entry_type

The id field for this should be entry_type_id not type_id

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs