简体   繁体   English

没有路由匹配[POST]“ /”-尝试设置邮件列表

[英]No route matches [POST] “/” - trying to set up mailing list

I feel like I am missing something simple here. 我觉得这里缺少一些简单的东西。 I set up a MailChimp mailing list and I am trying to get the sign up button to work but getting an error for the submission. 我设置了MailChimp邮件列表,并且试图使“注册”按钮正常工作,但提交时出错。 I have it routed back to the root in the create method in the controller but it isn't working. 我已将其路由回到控制器中create方法中的根目录,但无法正常工作。

signup.rb signup.rb

class Signup < ActiveRecord::Base
    validates_presence_of :email
    validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i

    def subscribe
        mailchimp = Gibbon::API.new
        result = mailchimp.lists.subscribe({
        :id => ENV['MAILCHIMP_LIST_ID'],
        :email => {:email => self.email},
        :double_optin => false,
        :update_existing => true,
        :send_welcome => true
        })
        Rails.logger.info("Subscribed #{self.email} to MailChimp") if result
    end

end

signups_controller.rb signups_controller.rb

class SignupsController < ApplicationController

    def new
        @signup = Signup.new
    end

    def create
        @signup = Signup.new(secure_params)
        if @signup.valid?
            redirect_to root_path
        else
            render :new
        end
    end

    private

    def secure_params
        params.require(:signup).permit(:email)
    end

end

routes.rb 的routes.rb

Rails.application.routes.draw do

  root 'pages#index'
  get '/about' => 'pages#about'
  get '/tour' => 'pages#tour'
  get '/music' => 'pages#music'

  resources :signups, only: [:new, :create]
end

What can I put in routes.rb to get this to post? 我可以在routes.rb中添加什么来发布? Here is my rake routes output... 这是我的耙路输出...

    Prefix Verb URI Pattern            Controller#Action
      root GET  /                      pages#index
     about GET  /about(.:format)       pages#about
      tour GET  /tour(.:format)        pages#tour
     music GET  /music(.:format)       pages#music
   signups POST /signups(.:format)     signups#create
new_signup GET  /signups/new(.:format) signups#new

Thanks in advance! 提前致谢!

Change 更改

<%= simple_form_for :signup do |f| %>

for 对于

<%= simple_form_for @signup do |f| %>

and you'll route to SignupsController#create . 然后您将路由到SignupsController#create

Apart from that, you need to call Signup#subscribe anywhere in your code for the whole form to work. 除此之外,您还需要在代码中的任意位置调用Signup#subscribe才能使整个表单正常工作。 Probably you want to save the Signup to the database as well. 可能您也想将Signup保存到数据库。 I suggest this change in the controller: 我建议在控制器中进行以下更改:

def create
    @signup = Signup.new(secure_params)
    if @signup.save && @signup.subscribe 
      redirect_to root_path
    else
      flash[:error] = 'Ooops!'
      render :new
    end
end

and remember to return true from Signup#subscribe if the result was successful. 并记住如果result成功,则从Signup#subscribe返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM