Search code examples
ruby-on-railsrubyruby-on-rails-3refinerycms

Rails why am i getting undefined method


I'm trying to create a form where a user can type in their email and it will save it to the db. I want this form in the footer of every page.

I've generated NewsletterSignup via rails scaffolding.

Now i have this code in my /app/views/refinery/_footer.html.erb:

<%= form_for(@newsletter_signup) do |f| %>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

and when i try and load the page i get this error:

 NoMethodError in Refinery/pages#home

Showing /Users/tomcaflisch/Sites/PersonalTrainingKT/app/views/refinery/_form.html.erb where line #1 raised:

undefined method `newsletter_signups_path' for #<#<Class:0x007fb84c769ba0>:0x007fb84c08d110>

Why is the method undefined? If i run rake routes i can see it exists:

newsletter_signups GET    /newsletter_signups(.:format)          newsletter_signups#index
                       POST   /newsletter_signups(.:format)          newsletter_signups#create
 new_newsletter_signup GET    /newsletter_signups/new(.:format)      newsletter_signups#new
edit_newsletter_signup GET    /newsletter_signups/:id/edit(.:format) newsletter_signups#edit
     newsletter_signup GET    /newsletter_signups/:id(.:format)      newsletter_signups#show
                       PUT    /newsletter_signups/:id(.:format)      newsletter_signups#update
                       DELETE /newsletter_signups/:id(.:format)      newsletter_signups#destroy

routes.rb

PersonalTrainingKT::Application.routes.draw do

  resources :newsletter_signups

  # This line mounts Refinery's routes at the root of your application.
  # This means, any requests to the root URL of your application will go to Refinery::PagesController#home.
  # If you would like to change where this extension is mounted, simply change the :at option to something different.
  #
  # We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery"
  mount Refinery::Core::Engine, :at => '/'
end

I'm also creating a new object in the application_controller since this newsletter signup will be available on every page:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :instantiate_newsletter_signup

  def instantiate_newsletter_signup
    @newsletter_signup = NewsletterSignup.new
  end
end

Solution

  • It looks like since refinerycms mounts the routes (not sure if that's correct terminology), it cannot see normal routes.

    From this link https://groups.google.com/forum/#!topic/refinery-cms/5k7Co4D1bVI I figured out that i needed to change

    <%= form_for(@newsletter_signup, :url => newsletter_signups_path, :method => :post) do |f| %>
    

    to

    <%= form_for(@newsletter_signup, :url => main_app.newsletter_signups_path, :method => :post) do |f| %>