ruby-on-rails - 通过 form_for 编辑自定义用户模型属性

标签 ruby-on-rails ruby-on-rails-4 devise

我目前正在尝试编辑页面上用户列中的属性,该属性不是设计默认值,它实际上位于名为 _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/

相关文章:

ruby-on-rails - OmniAuth 和 Devise,如何设置可选密码

ruby-on-rails - 使用设计时如何保留输入字段值

css - 耙 Assets :precompile bug with rails and compass and "*"

ruby-on-rails - 为什么我不能使用 RVM 在 Lion 上安装 Rails?

routes - 如何在 rails 4 中重新加载路由/config/routes/*?

ruby-on-rails - rails 4 : How to change the size of text_field

ruby-on-rails - 无法在 heroku 上使用 devise 登录,所有其他环境都可以

ruby-on-rails - Rails 4/Devise 3.00rc/pg "Unpermitted parameters"在注册/编辑

ruby-on-rails - 用于 Ruby on Rails 的 SAML 2.0 SSO?

ruby-on-rails - 设置 PayPal 自适应支付流程