# Tags to ruby interface: What I am missing? (repost)

**URL:** <https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039>\
**Category:** Rails\
**Created:** [March 15, 2006, 3:13am UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039 "2006-03-15T03:13:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elegant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@elegant](https://www.ruby-forum.com/u/elegant)\
**Post date:** [March 15, 2006, 3:13am UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039/1 "2006-03-15T03:13:47Z")

</div>

I offer my apologies in advance for the repost. On a list as busy as  
this, it is sometimes easy to get missed in the shuffle.

I am trying to create, display and edit a referenced object’s text field  
on the same view as my root object.

## For example:

Quiz: \<\<quiz.name\>\>  
Preamble: \<\<quiz.preamble\_presentation.text\>\>  
Postamble: \<\<quiz.postamble\_presentation.text\>\>

## [create][cancel]

quiz.preamble\_presentation references a Presentation object instance.  
The Presentation object contains a property called text, among others.  
The generated form will display the ID of the presentation, but I cannot  
figure out for the life of me how to display/edit the contents of the  
text property of the presentation.

What (I think) I want to display/edit is  
quiz.preamble\_presentation.text. However, this is an illegal construct  
according to the runtime environment.

I am missing something that will be patently obvious to someone who is  
comfortable with HTML tags.

My approach to taglibs in the past, with JSP’s, has been to dig out the  
generated servlet, throw away the JSP code and clean up the servlet code  
into something that is maintainable. This does not appear to be an  
(easy) option with rails, and I am open to becoming more comfortable  
with tag libs.

I am running under WEBrick with rails 1.0 and Ruby 1.8.4 under Fedora  
Core 3.

Thanks in advance,  
David J.

The quiz/\_form.rhtml contains:

\<%= error\_messages\_for ‘quiz’ %\>

Name  
\<%= text\_field 'quiz', 'name' %\>

Preamble presentation  
\<%= text\_field 'quiz', 'preamble\_presentation' %\>

Postamble presentation  
\<%= text\_field 'quiz', 'postamble\_presentation' %\>

The ddl for the tables is:

create table presentations  
(  
ID char(36) not null primary key,  
text varchar (1024),  
audio varchar (1024),  
visual varchar (1024)  
);

create table quizzes  
(  
ID char(36) not null primary key,  
name varchar (255) not null,  
preamble\_presentation char(36),  
postamble\_presentation char(36),  
foreign key (preamble\_presentation) references presentations(ID),  
foreign key (postamble\_presentation) references presentations(ID)  
);

---

<div class="post-metadata">

**Author:** ![elegant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@elegant](https://www.ruby-forum.com/u/elegant)\
**Post date:** [March 15, 2006, 3:59am UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039/2 "2006-03-15T03:59:23Z")

</div>

Is ‘text’ a reserved word? That would be my first guess. Try another  
column  
name instead of ‘text’.

Since you don’t post your code, it is impossible to tell if you are  
using  
invalid syntax.

---

<div class="post-metadata">

**Author:** ![elegant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@elegant](https://www.ruby-forum.com/u/elegant)\
**Post date:** [March 15, 2006, 4:08am UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039/3 "2006-03-15T04:08:33Z")

</div>

On Tue, 2006-03-14 at 18:57 -0800, HH wrote:

> Is ‘text’ a reserved word? That would be my first guess. Try another column  
> name instead of ‘text’.
> 
> Since you don’t post your code, it is impossible to tell if you are using  
> invalid syntax.

I am re-testing using textval.

Which piece of code do you need? That is part of the problem.

Since I am not yet fully comfortable with taglibs, I can’t tell which  
piece of code you might need. There’s a lot of generated classes so  
far.

Thanks,

---

<div class="post-metadata">

**Author:** ![elegant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@elegant](https://www.ruby-forum.com/u/elegant)\
**Post date:** [March 15, 2006, 4:11am UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039/4 "2006-03-15T04:11:34Z")

</div>

On Tue, 2006-03-14 at 18:57 -0800, HH wrote:

> Is ‘text’ a reserved word? That would be my first guess. Try another column  
> name instead of ‘text’.
> 
> Since you don’t post your code, it is impossible to tell if you are using  
> invalid syntax.

Does this error message help?

undefined method `preamble\_presentation.textvalue’ for  
#Quiz:0xb79261f0

Extracted source (around line #8):

5: \<%= text\_field ‘quiz’, ‘name’ %\>

  
6:  
7: 

Preamble  
presentation  
  
8: \<%= text\_field ‘quiz’, ‘preamble\_presentation.textvalue’ %\>

  
9:  
10: 

Postamble  
presentation  
  
11: \<%= text\_field ‘quiz’, ‘postamble\_presentation’ %\>

Related code snip from the controller:

def new  
@quiz = Quiz.new  
@quiz.preamble\_presentation = Presentations.new

# @quiz.postamble\_presentation = Presentations.new

end

def create  
@quiz = Quiz.new(params[:quiz])  
@quiz.preamble\_presentation =  
Presentations.new(params[:preamble\_presentation])

# @quiz.postamble\_presentation =

Presentation.new(params[:postamble\_presentation])  
if @quiz.preamble\_presentation.save and @quiz.save

# and @quiz.postamble\_presentation.save

```
  flash[:notice] = 'Quiz was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

```

end

---

<div class="post-metadata">

**Author:** ![elegant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@elegant](https://www.ruby-forum.com/u/elegant)\
**Post date:** [March 15, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039/5 "2006-03-15T20:44:19Z")

</div>

David,

A couple of things I would suggest.

First, how badly do you need that normalization? Can you put the  
preamble/postable directly in the quizzes table? If so, your problem is  
pretty much solved.

However, if that isn’t an option you have a couple of things to address.  
First, in your DDL you have both the premable\_presentaiton column AND a  
foreign key called preamble\_presentation. Rails isn’t going to like  
that.  
Rename or remove those non foreign\_key columns. Also rename your foreign  
key  
columns to something that rails expects: preamble\_presentation\_id and  
postamble\_presentation\_id

Next, you have to tell the model how these objects relate to each other.  
Assuming Quiz is your model class, you want to tell it how to find the  
preamble/postamble. The below assumes your presentatino table is  
represented  
as a Presentation active record, too:

class Quiz \< ActiveRecord::Base  
belongs\_to :preamble\_presentation, :class =\> “Presentation”  
belongs\_to :postamble\_presentation, :class =\> “Presentation”  
end

What this allows is direct access to the pre and postamble presentations  
associated with each quiz. ‘belongs\_to’ might seem odd, but you have to  
use  
it as the foreign key for your presentations actually resides in the  
quizzes  
table. If you had a foreign key to the quiz in the presentations table,  
you’d use the ‘has\_one’ keyword. Anyways, you can load a quiz and access  
those properties like so:

@quiz = Quiz.find\_by\_id(1)  
@quiz.preamble\_presentation.text = “foo”  
@quiz.postamble\_presentation.text = “bar”

Next, I wouldn’t bother with the text\_field methods in the view. I think  
you  
have stretched them farther than they want to go. Instead, use this  
pattern  
in your view:

\<%= text\_field\_tag ‘quiz[preamble\_presentation][text]’, @  
quiz.preamble\_presentation.text %\>

\<%= text\_field\_tag ‘quiz[postamble\_presentation][text]’, @  
quiz.postamble\_presentation.text %\>

Rails will interpret the ‘[]’ notation as nested properties within the  
object. In your your controller you can access these as nested hashes  
(assuming the form submit action is ‘update’):

def update  
@quiz = Quiz.find\_by\_id(:id, :include =\> [:preamble\_presentation,  
:postamble\_presentation])  
@quiz.preamble\_presentation.text =  
@params[:quiz][:preamble\_presentation][:text]  
@quiz.postamble\_presentation.text =  
@params[:quiz][:postamble\_presentation][:text]  
if @quiz.save && @quiz.preamble\_presentation.save && @  
quiz.postamble\_presentation.save  
flash[:message] = “Success!”  
redirect\_to :action =\> “show”, :id =\> @params[:id]  
else  
flash[:message] = “Unable to save.”  
end  
end

You have to call save 3 times because Rails will not automatically save  
objects in “belongs\_to” associations.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![elegant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@elegant](https://www.ruby-forum.com/u/elegant)\
**Post date:** [March 16, 2006, 12:52am UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039/6 "2006-03-16T00:52:37Z")

</div>

Thanks! This is exactly the sort of information I was looking for.

On Wed, 2006-03-15 at 11:41 -0800, Justin B. wrote:

> David,
> 
> A couple of things I would suggest.
> 
> First, how badly do you need that normalization? Can you put the  
> preamble/postable directly in the quizzes table? If so, your problem  
> is pretty much solved.

For the quizzer, it could be handled differently. For real projects  
involving financially sensitive data, it is very necessary. Since the  
quizzer is primarily a way for me to learn Rails with something that is  
immediately useful but not mission critical, I choose to lean towards  
learning rather than expediency.

> However, if that isn’t an option you have a couple of things to  
> address. First, in your DDL you have both the premable\_presentaiton  
> column AND a foreign key called preamble\_presentation. Rails isn’t  
> going to like that. Rename or remove those non foreign\_key columns.  
> Also rename your foreign key columns to something that rails expects:  
> preamble\_presentation\_id and postamble\_presentation\_id

The columns are the same. The Firebird understanding of this DDL is  
“the preamble\_presentation field is a foreign key from the presentations  
table’s ID field”, and it exists in this case primarily for the  
referential integrity during testing. I will rename the columns as you  
suggest.

Presentations is actually referenced by many more tables (Questions,  
Answers, HistoricalQuestions, and HistoricalAnswers for starters), so it  
cannot reflect ownership.

> Next, you have to tell the model how these objects relate to each  
> other. Assuming Quiz is your model class, you want to tell it how to  
> find the preamble/postamble. The below assumes your presentatino table  
> is represented as a Presentation active record, too:

It is …

> class Quiz \< ActiveRecord::Base  
> belongs\_to :preamble\_presentation, :class =\> “Presentation”  
> belongs\_to :postamble\_presentation, :class =\> “Presentation”  
> end

This looks sensible. If I am reading this correctly, it says “the  
instance property preamble\_presentation contains a reference to an  
instance of class Presentation”. The “belongs\_to” operator is used  
because the most common usage of this reference pattern is to indicate  
ownership in a hierarchy. My data model inverts some of the assumptions  
made by the rails designers, hence the backwards appearing construct.

It might be appropriate at some point in the future (after I am  
comfortable with the environment) for me to suggest that a “references”  
operator be added, which could be implemented as a synonym to  
belongs\_to, although its semantic meaning is subtly different.

> Next, I wouldn’t bother with the text\_field methods in the view. I  
> think you have stretched them farther than they want to go.

I figured as much. Since I am mostly unfamiliar with HTML tag  
programming, this is where I thought I was lost. Can you explain in  
more detail the semantics of this tag to the rhtml processing? I might  
understand it better then.

> (assuming the form submit action is ‘update’):  
> flash[:message] = “Success!”  
> redirect\_to :action =\> “show”, :id =\> @params[:id]  
> else  
> flash[:message] = “Unable to save.”  
> end  
> end

> You have to call save 3 times because Rails will not automatically  
> save objects in “belongs\_to” associations.

Makes sense - Rails does not pretend to understand my business, which is  
why it should be much easier to use than EJB’s.

> Hope that helps!  
> It sure does!

Thanks again!  
David J.

---

<div class="post-metadata">

**Author:** ![elegant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@elegant](https://www.ruby-forum.com/u/elegant)\
**Post date:** [March 16, 2006, 5:59am UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039/7 "2006-03-16T05:59:49Z")

</div>

My bad - I ran into that too and actually removed it from the code I  
was using to test my post. Just remove the :include statement from  
that find and it should work. Those two foreign keys that point to the  
same object confuse Rails somehow and it constructs a bad SQL  
statement.

---

<div class="post-metadata">

**Author:** ![elegant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@elegant](https://www.ruby-forum.com/u/elegant)\
**Post date:** [March 16, 2006, 4:12am UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039/8 "2006-03-16T04:12:53Z")

</div>

Almost success …

The lookup is failing:

code:  
def edit

# This failed - it generated some really funky and wierd SQL.

```
@quiz = Quiz.find_by_id(:id, :include =>

```

[:preamble\_presentation, :postamble\_presentation])  
end

error trace:

ActiveRecord::StatementInvalid in Admin#edit  
FireRuby::FireRubyException: Error preparing a SQL statement.  
Dynamic SQL Error  
SQL error code = -206  
Column unknown  
QUIZZES.PRESENTATION\_ID  
At line 1, column 480.  
Column does not belong to referenced table  
SQL Code = -206  
Firebird Code = 335544569  
: SELECT presentations.“TEXTVALUE” AS t2\_r1, presentations.“AUDIO” AS  
t2\_r2, presentations.“VISUAL” AS t2\_r3, presentations.“ID” AS t1\_r0,  
presentations.“TEXTVALUE” AS t1\_r1, quizzes.“ID” AS t0\_r0,  
presentations.“AUDIO” AS t1\_r2, quizzes.“NAME” AS t0\_r1,  
presentations.“VISUAL” AS t1\_r3, quizzes.“PREAMBLE\_PRESENTATION\_ID” AS  
t0\_r2, quizzes.“POSTAMBLE\_PRESENTATION\_ID” AS t0\_r3, presentations.“ID”  
AS t2\_r0 FROM quizzes LEFT OUTER JOIN presentations ON presentations.id  
= quizzes.presentation\_id LEFT OUTER JOIN presentations ON  
presentations.id = quizzes.presentation\_id WHERE (quizzes.“ID” = '—  
:id  
’ )

RAILS\_ROOT: script/…/config/…

Application Trace | Framework Trace | Full Trace  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/connection\_adapters/abstract\_adapter.rb:88:in  
`log' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/firebird_adapter.rb:315:in`execute’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/connection\_adapters/firebird\_adapter.rb:391:in  
`select' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/firebird_adapter.rb:306:in`select\_all’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/associations.rb:937:in  
`select_all_rows' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:851:in`find\_with\_associations’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/base.rb:395:in  
`find' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:393:in`find’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/base.rb:980:in  
`method_missing' ./script/../config/../app/controllers/admin_controller.rb:52:in`edit’

Here’s the code examples you gave me, corrected so that they compile and  
are starting to work:

On Wed, 2006-03-15 at 11:41 -0800, Justin B. wrote:

> is represented as a Presentation active record, too:  
> class Quiz \< ActiveRecord::Base  
> require ‘presentations’  
> require ‘question’

include UUIDHelper

validates\_presence\_of :name  
has\_many :questions, :order =\> :position  
belongs\_to :preamble\_presentation, :class\_name =\> “Presentations”  
belongs\_to :postamble\_presentation, :class\_name =\> “Presentations”  
end

> Next, I wouldn’t bother with the text\_field methods in the view. I  
> think you have stretched them farther than they want to go. Instead,  
> use this pattern in your view:

Preamble presentation  
\<%= text\_field\_tag 'quiz[preamble\_presentation][textvalue]', @quiz.preamble\_presentation.textvalue %\>

Postamble presentation  
\<%= text\_field\_tag 'quiz[postamble\_presentation][textvalue]', @quiz.postamble\_presentation.textvalue %\>

> Rails will interpret the ‘[]’ notation as nested properties within the  
> object. In your your controller you can access these as nested hashes  
> (assuming the form submit action is ‘update’):

class AdminController \< ApplicationController

def index  
list  
render :action =\> ‘list’  
end

def list\_questions  
redirect\_to(:controller =\> “/questions”, :parent\_quiz =\> params  
[:id])  
end

def list  
@quiz\_pages, @quizzes = paginate :quizzes, :per\_page =\> 10  
end

def show  
@quiz = Quiz.find\_by\_id(:id, :include =\>  
[:preamble\_presentation, :postamble\_presentation])  
end

def new  
@quiz = Quiz.new  
@quiz.preamble\_presentation = Presentations.new  
@quiz.postamble\_presentation = Presentations.new  
@quiz.preamble\_presentation.textvalue = ‘’  
@quiz.postamble\_presentation.textvalue = ‘’  
end

def create

# changed because we are instantiating the presentations here

# may not be needed if we instantiate presentations in the initialize,

# but that will require thinking because we already have a use for

# the initialize method.

# @quiz = Quiz.new (@params[:quiz])

```
@quiz = Quiz.new
@quiz.name = @params[:quiz][:name]
@quiz.preamble_presentation = Presentations.new
@quiz.postamble_presentation = Presentations.new
@quiz.preamble_presentation.textvalue = @params

```

[:quiz][:preamble\_presentation][:textvalue]  
@quiz.postamble\_presentation.textvalue = @params  
[:quiz][:postamble\_presentation][:textvalue]

```
if @quiz.preamble_presentation.save &&

```

@quiz.postamble\_presentation.save && @quiz.save

```
  flash[:notice] = 'Quiz was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

```

end

def edit  
@quiz = Quiz.find(params[:id])  
end

def update  
@quiz = Quiz.find\_by\_id(:id, :include =\>  
[:preamble\_presentation, :postamble\_presentation])  
@quiz.preamble\_presentation.textvalue = @params  
[:quiz][:preamble\_presentation[:textvalue]]  
@quiz.postamble\_presentation.textvalue = @params  
[:quiz][:postamble\_presentation[:textvalue]]  
if @quiz.preamble\_presentation.save &&  
@quiz.postamble\_presentation.save && @quiz.save  
flash[:notice] = ‘Quiz was successfully updated.’  
redirect\_to :action =\> “show”, :id =\> @params[:id]  
else  
flash[:message] = “Unable to save.”  
render :action =\> ‘edit’  
end  
end

def destroy  
@quiz = Quiz.find\_by\_id(:id, :include =\>  
[:preamble\_presentation, :postamble\_presentation])  
@quiz.preamble\_presentation.destroy  
@quiz.postamble\_presentation.destroy  
@quiz.destroy  
redirect\_to :action =\> ‘list’  
end  
end

---

<div class="post-metadata">

**Author:** ![elegant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@elegant](https://www.ruby-forum.com/u/elegant)\
**Post date:** [March 16, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/tags-to-ruby-interface-what-i-am-missing-repost/82039/9 "2006-03-16T13:31:30Z")

</div>

Here’s how to make it work - add the :foreign\_key directive

class Quiz \< ActiveRecord::Base  
require ‘presentations’  
require ‘question’

include UUIDHelper

validates\_presence\_of :name  
has\_many :questions, :order =\> :position  
belongs\_to :preamble\_presentation, :class\_name =\>  
“Presentations”, :foreign\_key =\> :preamble\_presentation\_id  
belongs\_to :postamble\_presentation, :class\_name =\>  
“Presentations”, :foreign\_key =\> :postamble\_presentation\_id  
end
