看起来我已经在代码中弄清楚了所有内容,但是我总是收到未初始化的常量错误。在我的route.rb中,我有:
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
在我的控制器subcribers_controller.rb文件中,我具有:
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
在视图文件中:
<%= 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 %>
我卡住了,不知道如何解决。我阅读了有关该问题的类似主题,但没有帮助。
答案 0 :(得分:2)
我认为您在控制器文件名中缺少s
:subcribers_controller.rb
-> subscribers_controller.rb