jeudi 23 février 2017

Connect Multiple Yields in Rails

Rails Newbie. Be gentle. If I need to show more stuff I'll do it.

Trying to insert a newsletter signup block above my footer on a project but didn't make it a partial in the layouts set up.

I have the yield outputting an index from a blog.

Is it possible to have multiple yields to different indexes?

Is it possible to insert another page into a layout page?

application.html.erb

<div id="blog">
  <%= yield %>
</div>
<div>
 <%= content_for?(:newsletter) ? yield(:newsletter) : yield %>
</div>
<div>
 <%= render 'layouts/footer' %>
</div>

newsletter.html.erb

<% content_for :newsletter do %>
<h1>Get My Awesome News Letter</h1>
<p>Give me your email and keep up to date on my cat's thoughts.</p>
<%= form_tag('/emailapi/subscribe', method: "post", id: "subscribe", remote: "true") do -%>
   <%= email_field(:email, :address, {id: "email", placeholder: "email address"}) %>
   <%= submit_tag("Sign me up!") %>
<% end %>

emailapi_controller.rb

class EmailapiController < ApplicationController
def newsletter
    render params[:newsletter]
end

def subscribe
    gb = Gibbon::Request.new

    gb.lists.subscribe({
        :id => ENV["MAILCHIMP_LIST_ID"],
        :email => {:email => params[:email][:address]}
        })
end
end

routes.rb

root to: 'posts#index'

get "/:newsletter" => 'emailapi#newsletter'
post 'emailapi/subscribe' => 'emailapi#subscribe'

Aucun commentaire:

Enregistrer un commentaire