# Working through an Extension

**URL:** <https://www.ruby-forum.com/t/working-through-an-extension/111260>\
**Category:** Radiant CMS\
**Created:** [August 4, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/working-through-an-extension/111260 "2007-08-04T14:54:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 4, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/working-through-an-extension/111260/1 "2007-08-04T14:54:22Z")

</div>

Hi Guys,

After my request for help towards figuring out how to not include a  
stylesheet in the menu, I thought it would be quite good to try to  
create an extension that allows me to specify a property which I could  
use to add pages to a menu.

I read through most of how the PageAttachments extension works and it’s  
really neat (in my opinion) how it extends the Page model and the way  
that the Edit and New pages are changed to include fields to attach  
files, etc. I’ve set up the extension in exactly the same way as  
PageAttachments, but I’m facing a problem in trying to add items to the  
database.

Basically, I have an after\_save filter that gets called when you try to  
save the page. All I need to figure out now is how to get the page ID  
of the record that was just saved, and more importantly, the parameters  
that were entered into the partial added to the new and edit page.

If I understand it correctly, the PageAttachments uses AJAX for  
uploading files to a temporary location and then saves them in the  
after\_save filter by iterating through an array that is populated with  
the names of the files that were uploaded. I don’t need something that  
complex (and I’d prefer not to use AJAX), so I’m wondering how to access  
the text field! (Let’s say I have a text field called ‘property’ that I  
would like to add). I know that this may be a Rails issue (rather than  
specific to Radiant), but I’m hoping someone can point me in the correct  
direction with this.

Thanks for all the patience, guys!

Cheers,  
Mohit.  
8/4/2007 | 8:52 PM.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 4, 2007, 6:17pm UTC](https://www.ruby-forum.com/t/working-through-an-extension/111260/2 "2007-08-04T18:17:40Z")

</div>

> Basically, I have an after\_save filter that gets called when you try to  
> save the page. All I need to figure out now is how to get the page ID  
> of the record that was just saved, and more importantly, the parameters  
> that were entered into the partial added to the new and edit page.

You’re on the right track. It’s easiest if the model is associated with  
Page, then you can just push the new objects into the association  
collection and not worry about the page id. Otherwise, it’s available  
via self.id inside that callback. Add an attr\_accessor with some  
variable-name that you check in the after\_save callback… it’ll just be  
a hash with the attributes passed into the form, which you can pass on  
to your associated model.

> 

PageAttachments uses the magic of attachment\_fu to do the file  
handling. There’s no Ajax, but there’s some client-side Javascript to  
add and remove elements from the page.

Let me know if there’s anything else I can do to help!

Sean

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 4, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/working-through-an-extension/111260/3 "2007-08-04T18:28:41Z")

</div>

Thanks Sean.

> > Basically, I have an after\_save filter that gets called when you try  
> > to save the page. All I need to figure out now is how to get the  
> > page ID of the record that was just saved, and more importantly, the  
> > parameters that were entered into the partial added to the new and  
> > edit page.
> 
> You’re on the right track.

That’s good to know. 🙂

> It’s easiest if the model is associated with Page, then you can just  
> push the new objects into the association collection and not worry  
> about the page id. Otherwise, it’s available via self.id inside that  
> callback. Add an attr\_accessor with some variable-name that you check  
> in the after\_save callback… it’ll just be a hash with the attributes  
> passed into the form, which you can pass on to your associated model.

Ah, the attr\_accessor - I just noticed that that was the part I had  
commented out from your extension. I think once I put that in, I should  
probably be able to get the string that my user entered.

> handling. There’s no Ajax, but there’s some client-side Javascript to  
> add and remove elements from the page.

I just looked at the JS files and assumed it was Ajax - my bad.

> Let me know if there’s anything else I can do to help!

Teach me to fish! 🙂 The ability to add extensions is useful - I’d like  
to learn. But, seriously, thanks for pointing me in the correct  
direction. I’m sure I’ll probably (that sounds strange) come back with  
a few more questions in the next few days (hours?). I think it’s time  
to get a copy of ‘Ruby for Rails’ and read up on meta-programing 🙂

Cheers,  
Mohit.  
8/5/2007 | 12:25 AM.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 4, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/working-through-an-extension/111260/4 "2007-08-04T19:01:43Z")

</div>

Sean C. wrote:

> via self.id inside that callback. Add an attr\_accessor with some  
> variable-name that you check in the after\_save callback… it’ll just  
> be a hash with the attributes passed into the form, which you can pass  
> on to your associated model.

Hi Sean,

With a little help from you and LiveHTTPHeaders (extension for Firefox)  
and adding in attr\_accessor :user\_text and changing the form field name  
to page[user\_text], I’m now finally able to access the user text in my  
after\_save filter.

Now, to figure out what I want to do with the text that the user  
entered. I could store that to the database by creating a new object.  
I’ll now also look into your directly pushing the new objects into the  
association collection.

On the other hand, a Property field may be best handled as a drop down  
list that the user can select from and creating a ‘belongs\_to’  
association.

Just one more question if I may: what is the recommended practice for  
adding columns to the Pages table? Should I do an add\_column and create  
a migration in my extension for it or should I just create a separate  
table?

Thanks again!

Cheers,  
Mohit.  
8/5/2007 | 12:54 AM.
