我正在尝试创建一个简单的按钮,当用户提交表单时,会将布尔user
属性notify_generator
从其默认值false
更新为true
。该表单位于我的home#construction
页面上:
<% if current_user.notify_generator == false %>
<h1 class="">Want to know when it's ready?</h1>
<%= form_for(@user) do |f| %> <<<<<<ERROR CALLED ON THIS LINE
<%= f.hidden_field :notify_generator, value: true %>
<%= f.submit "Keep Me In The Loop", class: "btn btn-primary" %>
<% end %>
<% else %>
<h1>You're set! We'll let you know when the generator is up and running!</h1>
<% end %>
home_controller
如下:
class HomeController < ApplicationController
before_action :historical_only
skip_before_action :historical_only, except: [:history]
before_action :authenticate_user!, only: [:construction, :generator]
def index
end
def history
end
def about
end
def gear
end
def generator
end
def construction
@user = current_user
end
private
def historical_only
unless current_user && ( current_user.access_historical == true || current_user.admin == true )
flash[:error] = "You must be a historical BurnIt member to see those goodies."
render action: "index"
end
end
end
现在,当我尝试访问home#construction
页面时,我得到undefined method 'user_path' for #<#<Class:0x007fbc236054a0>:0x007fbc24495470>
。我认为这是尝试从注册控制器的域外更新用户的症状。我使用devise
宝石,如果这有所不同。
有谁能看到我在这里出错的地方?
其他信息:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
答案 0 :(得分:1)
您有两种选择:您既可以定义自己的UsersController,也可以使用Devise现有的RegistrationsController。
我不会解释如何实现UsersController - 这是标准的MVC,我相信你以前已经完成了。
但是如果你想利用现有的设计代码,那么让我们来看看这个here的设计方式:
<%= form_for(resource, as: resource_name,
url: registration_path(resource_name), html: { method: :put }) do |f| %>
请注意,您需要传递选项url: registration_path(resource_name)
。在这种情况下,这会解析为user_registration_path
,因此您只需使用url: user_registration_path
即可。 resource
部分专门用于设计控制器 - 因为您使用的是@user
,因此您不需要as: resource_name
,因为Rails可以推断出资源名称为user
}来自实例变量(@user
)的名称。
您可能需要自定义允许的参数。有关如何执行此操作,请参阅devise README。