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.
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!
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.