How to have an object in every Rails action?
I'm building a new site in Rails, and in the footer of every page there is a newsletter sign-up form. (the form is part of the application.html.erb template) In the "index" action for the home page, I have @email = EmailNewsletterMember.new to provide an object for form_for to work with. I just now realized that in order to have this work, EVERY action for EVERY page will have to have this code. I k开发者_StackOverflow中文版now there has got to be a correct way to do this, can someone please enlighten me?
Try this
class ApplicationController < ActionController::Base
before_filter { @email = EmailNewsletterMember.new }
...
end
You could also potentially just throw EmailNewsletterMember.new directly into your form_for but only if there's nothing else you need to do with the object.
form_for(EmailNewsletterMember.new) do |f|
...
You can put that in a before_filter in ApplicationController.
加载中,请稍侯......
精彩评论