我有一个Subscriber
模型,它接受一个“phone_number”和一个“visit”整数。我有两个控制器Subscribers
和Visits
(超级和子级)我以前从未使用过嵌套控制器,我对命名空间有一些问题我相信。因为我收回了未初始化的常量错误。基本上,用户控制器注册用户,并且访问控制器通过用户输入他们的phone_number来计算他们访问过的次数。为什么我收到此错误?为了清楚起见,我会展示我的代码。
控制器
class Subscribers::VisitsController < ApplicationController
def new
@subscriber = Subscriber.new
end
def create
@subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if @subscriber
@subscriber.visit += 1
@subscriber.save
redirect_to subscribers_visits_new_path(:subscriber)
else
render "new"
end
end
end
class SubscribersController < ApplicationController
def index
@subscriber = Subscriber.all
end
def new
@subscriber = Subscriber.new
end
def create
@subscriber = Subscriber.create(subscriber_params)
if @subscriber.save
flash[:success] = "Subscriber Has Been successfully Created"
redirect_to new_subscriber_path(:subscriber)
else
render "new"
end
end
路线
Rails.application.routes.draw do
devise_for :users
resources :subscribers, except: :show
get '/subscribers/visits/new', to: 'subscribers/visits#new'
root "welcomes#index"
视图
<h1>hey</hey>
<%= form_for @subscriber do |form| %>
<div class="form-group">
<p>
<%= form.label :phone_number %>
<%= form.text_field :phone_number %>
</p>
<% end %>
ERROR
答案 0 :(得分:2)
嗯,我的猜测是你试图在subscriber/visits/new
中将网址new
发送到VisitsController
行动?如何更改此行:
get '/subscribers/visits/new', to: 'subscribers/visits#new'
到:
namespace :subscribers do
get '/visits/new', to: 'visits#new'
end
如果仍然出现错误,请尝试将此块移至resources :subscribers, except: :show
以上。
干杯
答案 1 :(得分:1)
您可能不需要从另一个控制器继承一个控制器。只需像往常一样定义控制器:
应用/控制器/ subscribers_controller.rb 强>
class SubscribersController < ApplicationController
# methods for Subscribers
end
app / controllers / visits_controller.rb 中的
请注意,这些必须位于单独的文件中,以便Rails可以通过其查找的对象的名称找到正确的源文件。这是一个Rails命名约定。 关于您的路线,您需要更改为使用4种路线格式之一。阅读Adding More RESTful Actions指南中关于Rails Routing from the Outside In的部分可能有所帮助。 1)要将 这将产生这些路线: 这是嵌套资源和独立控制器的典型路由结构。 2)要在VisitsController中使 这将产生这些路线: 这通常用于在现有资源和控制器中添加新的顶级路由。 3)要将 这将产生这些路线: 这通常用于在现有资源和控制器中添加新成员路由。 4)要简单地将 这将产生这些路线: 这可用于使任意路由看起来包含在现有资源中,而它们实际上可能是独立的。class VisitsController < ApplicationController
# methods for Visits
end
visits
作为嵌套资源进行路由,这就是您实际尝试做的事情,您可以使用它:resources :subscribers, except: :show do
resources :visits
end
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
GET /subscribers/:id/visits
GET /subscribers/:id/visits/:id
GET /subscribers/:id/visits/:id/edit
PATCH /subscribers/:id/visits/:id/update
DELETE /subscribers/:id/visits/:id/destroy
visits#new
成为简单的集合(非成员)操作,您可能会想要这样:resources :subscribers, except: :show do
collection do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/visits/new
POST /subscribers/visits
visits
构建为成员操作,请使用以下命令:resources :subscribers, except: :show do
member do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
visits
路由显示为subscribers
,您可以使用此功能:get '/subscribers/visits/new', to: 'visits#new'
post '/subscribers/visits', to: 'visits#create'
resources :subscribers, except: :show
GET /subscribers/visits/new
POST /subscribers/visits
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy