从其他控制器调用设计资源对象

时间:2013-10-10 10:15:33

标签: ruby-on-rails-3.2 devise

我的应用程序中有密码更新。我必须从其他控制器调用相同的表单。我添加了相同的表单,但它给了我错误未定义的局部变量或方法`resource'。如何从任何其他控制器调用更新密码? 我的表格是:

<%= simple_form_for(resource, as: resource_name, url: update_password_registrations_path(resource), html: { method: :put }, remote: true) do |f| %>
  <div class="form-inputs">
  <%= f.input :current_password, required: true, wrapper: :prepend, error: true do %>
    <span class="add-on"><i class="ss-icon ss-key"></i></span>
    <%= f.input_field :current_password, placeholder: "Old Password" %>
  <% end %>
  <%= f.input :password, required: true, wrapper: :prepend, error: true do %>
      <span class="add-on"><i class="ss-icon ss-key"></i></span>
      <%= f.input_field :password, placeholder: "New Password" %>
  <% end %>
  <%= f.input :password_confirmation, required: true, wrapper: :prepend, error: true do %>
        <span class="add-on"><i class="ss-icon ss-key"></i></span>
        <%= f.input_field :password_confirmation, placeholder: "New Password Confirmation" %>
  <% end %>
</div>
  <%= f.button :submit, "Update", class: "btn btn-primary" %>
<% end %>

1 个答案:

答案 0 :(得分:1)

您的资源就是您所谓的设计资源。所以它可能是用户。

替换

<%= simple_form_for(resource, as: resource_name, url: update_password_registrations_path(resource), html: { method: :put }, remote: true) do |f| %>

<%= simple_form_for(@user, url: update_password_registrations_path(@user), html: { method: :put }, remote: true) do |f| %>

确保在控制器中设置@user。如果它是当前用户的表单,您可以将@user替换为current_user