0
votes

It looks I made everything clear im my code, but I'm always receiving uninitialized constant error. In my routes.rb I have:

Rails.application.routes.draw do
  get 'welcome/index'

  resources :articles do
    resources :comments
    collection do
      get :search #creates a new path for searching
    end
  end
  resources :subscribers
  root 'welcome#index'

end

In my controller subcribers_controller.rb file I have:

class SubscribersController < ApplicationController
  def index
  end

  def create
    @subscriber = Subscriber.new(subscriber_params)
#check if email exists in database
      if Subscriber.exists?(email: @subscriber.email)
        redirect_to root_path, alert:
        "Sorry, that email already exists!"
      elsif @subscriber.save
        redirect_to root_path, notice:
        "Thank you #{@subscriber.f_name}, for subscribing to my Newslatter!"
      else
        redirect_to root_path, alert:
        "Sorry, I failed to save your information. Please, try again!"
      end
  end

  private

    def subscriber_params
      params.require(:subscriber).permit(:f_name, :l_name, :email, :country)
    end
end

And in view file:

  <%= form_for :subscriber, url: subscribers_path do |f| %>
      <%= f.text_field :f_name, placeholder: "First Name",
                         class:"form-control mr-sm-2" %>
      <%= f.text_field :l_name, placeholder: "Last Name",
                         class:"form-control mr-sm-2" %>
      <%= f.email_field :email, placeholder: "Your Email",
                         class:"form-control mr-sm-2" %>
      <%= f.select :country,
            options_for_select(['Country1', 'County2', 'County3', 'County4']),
            prompt: 'Select your Country' %>
      <%= f.submit 'Submit', class:"btn btn-light my-2 my-sm-0" %>
      <% end %>

And I stucked and don't know how to solve it. I read similar topics about that issue, and it didn't help.

1

1 Answers

1
votes

I think you're missing a s in your controller filename: subcribers_controller.rb -> subscribers_controller.rb