Skip to content

Instantly share code, notes, and snippets.

@nesquena
Created December 26, 2008 00:49
Show Gist options
  • Save nesquena/39984 to your computer and use it in GitHub Desktop.
Save nesquena/39984 to your computer and use it in GitHub Desktop.
# /app/views/families/show.html.haml
= link_to 'Send Message', new_family_message_path(:family_id => @family.id, :category => 'Family')
# /app/controllers/messages_controller.rb
def new
@category = params[:category]
render :action => "messages/#{@category}_form", :layout => true
end
# /app/views/messages/_family_form.html.haml
- form_for :message,:url => new_family_messages_path(:family_id => X) do |f|
= f.text_field :subject
= f.text_field :body
= f.submit
= hidden_field_tag :category, @category
# /app/controllers/messages_controller.rb
# params => { :xxxxx_id => x, :category => 'xxxx', :message => { ... } }
def create
@category = params[:category] # => 'Family'
@parent_id = params["#{@category.downcase}_id"] # => params[:family_id]
@messageable=@category.constantize.find(@parent_id) # => Family.find(...)
@messageable.messages.build(params[:message])
#.... save and shit
end
# Resources
# * http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment