我目前正在尝试编辑页面上用户列中的属性,该属性不是设计默认值,它实际上位于名为 _withdraw.html.erb 的部分中,我不断收到错误:
#<#:0x007fd4297ab408> 的未定义方法“user_path”
我不明白如何解决这个问题。
_withdraw.html.erb
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="col-md-12">
<div class="form-wrapper">
<div class="form-group">
<label for="paypal-email">Your Paypal Email Address</label>
<%= f.text_field :paypal %>
<p class="help-block">Example block-level help text here.</p>
<%= f.submit %>
</div>
</div>
</div>
</div>
<% end %>
任何帮助都会很棒。预先感谢您
编辑
路线.rb
Rails.application.routes.draw do
devise_for :users
resources :offers
resources :categories, only: :show
root 'welcome#index'
get '/balance', to: 'balance#show', as: 'balance'
end
balance_controller.rb
class BalanceController < ApplicationController
def show
@user = current_user
end
end
rake 路线
Prefix Verb URI Pattern Controller#Action
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) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
offers GET /offers(.:format) offers#index
POST /offers(.:format) offers#create
new_offer GET /offers/new(.:format) offers#new
edit_offer GET /offers/:id/edit(.:format) offers#edit
offer GET /offers/:id(.:format) offers#show
PATCH /offers/:id(.:format) offers#update
PUT /offers/:id(.:format) offers#update
DELETE /offers/:id(.:format) offers#destroy
category GET /categories/:id(.:format) categories#show
root GET / welcome#index
balance GET /balance(.:format) balance#show
最佳答案
要添加到其他答案,
您必须记住,调用部分意味着您必须对要从中访问的数据变量格外警惕。
换句话说,调用 @user
是不好的做法。 您要么必须限制部分的使用,这会否定它的使用 - IE 是 antipattern ,或者必须定义 @user
每次加载应用程序时,这都非常非常糟糕。
设计
由于您使用的是 Devise,因此有一些方法可以解决您的问题。
<强> current_user
您需要考虑重构您的代码(不要部分依赖@user
)。此外,您需要确保在表单中使用正确的数据。
这是一个开始:
<%= form_for current_user, path: edit_user_registration_path, method: :put do |f| %>
<% if current_user.errors.any? %>
....
<% end %>
设计填充 current_user
每次您发送新请求时。这意味着您可以使用current_user
而不是@user
在这种情况下。
--
您还需要在form_for
中定义您自己的路径 helper 。
正如另一个答案中提到的, form_for
助手根据 object
填充表单你通过了。通常,它使用 RESTful 路由模式来确定要定义的路径:
来自API :
The URL the form is to be submitted to. This may be represented in the same way as values passed to url_for or link_to. So for example you may use a named route directly. When the model is represented by a string or symbol, as in the example above, if the :url option is not specified, by default the form will be sent back to the current url (We will describe below an alternative resource-oriented usage of form_for in which the URL does not need to be specified explicitly).
这意味着虽然你可能已经通过了 current_user
,缺乏真正的 RESTful 路由结构将阻止 Rails 正确填充表单代码。通常,您会有一个 users/edit
路径,这是 Devise 所缺少的。
通过定义您自己的路径(IE edit_user_registration_path
使用 PUT
方法),您将能够访问您需要的路径。
--
最后,您需要查看 Devise 允许您 access Devise objects around your app 的规定.
具体:
#app/helpers/application_helper.rb
class ApplicationHelper
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
end
这将允许您调用 <%= form_for resource_name, resource, :url => registration_path(resource_name) do |f| %>
关于ruby-on-rails - 通过 form_for 编辑自定义用户模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32656491/