开发者

How to have an object in every Rails action?

开发者 https://www.devze.com 2023-03-13 13:33 出处:网络
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'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.

0

精彩评论

暂无评论...
验证码 换一张
取 消