I have a model called Post in my Rails 3 app.
Users can create their own urls for these posts. (I call this a clean_url.)
If the user does not complete this field, I want to create the value myself from the title upon saving the form. (Essentially use the @post.title.to_s(some reg ex to remove spaces etc…)
What is the best approach to having the item save with title value in this field if it is left blank?
I assumed in the Posts controller create action I could “update_attributes” of the post upon saving… but Im beginning to think maybe this is wrong?
Anyone have ideas on how to achieve this?
Use a
before_savecallback, and a private method in your model. The following is taken straight from my blog source.