开发者

Newsletter signup form in the main layout

开发者 https://www.devze.com 2023-02-08 23:10 出处:网络
I created a model N开发者_如何学编程ewsletter with newsletter controller: class NewslettersController < ApplicationController

I created a model N开发者_如何学编程ewsletter with newsletter controller:

class NewslettersController < ApplicationController


  def signup
    @signup=Newsletter.new(params[:newsletter])
    @signup.save
  end

end

I want call this to the layouts/application.html.erb.

I created a partial newsletters/_signup:

 <%= form_for @newsletter, :html => {:multipart => true} do |f| -%>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
     <%= f.submit 'Go!' %>
<% end -%>

But when I use render :partial => "newsletters/signup" I got undefined method `model_name' for NilClass:Class.

Whats wrong? (Rails 3)


There is much confusion here:

if you add something in your application layout, it should be rendered for all pages. So the variable should be created everywhere -> that's the application_controller job.

In your application_controller.rb, add:

before_filter :mailing_list

def mailing_list
  @newsletter = Newsletter.new
end

And remove your definition of signup in your Newsletter controller.

0

精彩评论

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