【问题标题】:Updating user information outside devise pages在设计页面外更新用户信息
【发布时间】:2016-07-27 11:47:27
【问题描述】:

我想为用户添加从 /settings 页面而不是 /users/edit 页面编辑他的电子邮件/密码/图像的选项。所以我把代码复制到设置页面

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= devise_error_messages! %>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
  <% end %>

  <div class="form-group">
    <%= f.label :image %>
    <%= f.file_field :image, class: 'form-control'%>
  </div>

  <div class="form-group">
    <%= f.label :cover %>
    <%= f.file_field :cover, class: 'form-control'%>
  </div>

  <div class="field">
    <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
    <%= f.password_field :current_password, autocomplete: "off" %>
  </div>

  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>

<h3>Cancel my account</h3>

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>

<%= link_to "Back", :back %>

但它返回了这个

undefined local variable or method `resource' for #<#<Class:0x007f60245ab9b0>:0x007f6026f58358>

我应该向控制器添加什么?

【问题讨论】:

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


    【解决方案1】:

    当您想要执行任何自定义操作时,首先创建一个action in a controller,然后为它创建一个route,然后是view file

    资源对象仅在设计路线中可用。

    所以,首先在任何控制器中添加一个名为 settings 的方法,例如:userscontroller。

    class UsersController < ApplicationController
        def settings
            @user = current_user
        end
    end
    

    然后在 routes.rb 中,添加一个指向这个动作的路由,路由命名为'/settings'

    get '/settings' => 'users#settings', as: :settings
    

    现在,在 app/views/users/settings.html.erb

    <%= form_for(@user, url: registration_path(@user), html: { method: :put }) do |f| %>
          <%= devise_error_messages! %>
          <div class="field">
            <%= f.label :email %><br />
            <%= f.email_field :email, autofocus: true %>
          </div>
    
          <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
            <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
          <% end %>
    
          <div class="form-group">
            <%= f.label :image %>
            <%= f.file_field :image, class: 'form-control'%>
          </div>
    
          <div class="form-group">
            <%= f.label :cover %>
            <%= f.file_field :cover, class: 'form-control'%>
          </div>
    
          <div class="field">
            <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
            <%= f.password_field :password, autocomplete: "off" %>
          </div>
    
          <div class="field">
            <%= f.label :password_confirmation %><br />
            <%= f.password_field :password_confirmation, autocomplete: "off" %>
          </div>
    
          <div class="field">
            <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
            <%= f.password_field :current_password, autocomplete: "off" %>
          </div>
    
          <div class="actions">
            <%= f.submit "Update" %>
          </div>
        <% end %>
    
    <h3>Cancel my account</h3>
    
    <p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
    
    <%= link_to "Back", :back %>
    

    所以,在用户的显示页面中添加一个指向设置页面的链接,

    <%= link_to 'Edit', settings_path(user) %>
    

    【讨论】:

    • 仍然为#:0x007fb488d4e290>`返回undefined local variable or method resource'。首先,返回未定义的用户,所以我用当前用户替换它,然后我得到了同样的错误
    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2011-06-23
    相关资源
    最近更新 更多