【问题标题】:changing devise route after edit user page编辑用户页面后更改设计路线
【发布时间】:2014-09-12 21:48:02
【问题描述】:

我希望我的用户在登陆他们的编辑用户/更新帐户页面后能够直接进入路径订阅/新。任何人都可以帮助我如何解决这个问题。我在下面列出了我的路线

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

   before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :line1, :line2, :town, :county, :postcode, :password, :password_confrimation, :current_password)}
  end

 private
  def after_sign_in_path_for(resource)
    edit_user_registration_path(current_user) #basically whichever path you think meets your needs
  end

routes.rb文件

  Rails.application.routes.draw do

  resources :subscriptions, only: [:new, :create, :show, :destroy]
  #gives standard routes

  get 'content/fruit'

  get 'content/veg'

  get 'content/mix'

  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
  namespace :admin do
  get 'dashboard/index'
  end

  devise_for :users, :controllers => {:registrations => 'devise/registrations'}
   # devise_for :users
   resources :products do 
   resources :orders, only: [:new, :create]
   #tells rails needs product id number
  end

  # get 'pages/payment'

  get 'home/about'

  get 'home/contact'

  get 'seller' => "products#seller"

  get 'sales' => "orders#sales"

  get 'static_pages/productlanding'

  get "content/veg"

  get "content/fruit"

  get "content/mix"

  get 'subscriptions/new'

设计注册控制器

class Devise::RegistrationsController < DeviseController

  def update
    set_flash_message :notice, :"message here" if is_flashing_format?
    session[:user_return_to] = new_subscription_path
    super  
  end
end

编辑表单用户

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= devise_error_messages! %>


  <div class="form-group">
    <%= f.label :avatar, class: 'col-sm-2 control-label'  %>
    <div class="col-sm-6">
      <%= f.file_field :avatar %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :name %>
    <%= f.text_field :name, autofocus: true, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :email %>
    <%= f.email_field :email, class: "form-control" %>
  </div>

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

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

  <div class="form-group">
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i>
    <%= f.password_field :current_password, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :line1 %>
    <%= f.text_field :line1, autofocus: true, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :line2 %>
    <%= f.text_field :line2, autofocus: true, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :town %>
    <%= f.text_field :town, autofocus: true, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :county %>
    <%= f.text_field :county, autofocus: true, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :postcode %>
    <%= f.text_field :postcode, autofocus: true, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :organization %>
    <%= f.text_field :organization, autofocus: true, class: "form-control" %>
  </div>


  <div class="form-group">
    <%= f.submit "Update", class: "btn btn-primary" %>
  </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, class: "btn btn-danger" %></p>

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

【问题讨论】:

  • 你在你的方法中update之后写了redirect_to ...._path
  • 当您提到“直接来自编辑/更新”时,您是什么意思?您想在 操作执行后重定向,还是更改路由本身?
  • 是的,我的意思是在执行操作后重定向。对不起,对 Rails 很陌生..
  • nithin 你能扩展一下你的评论吗?我不太明白你的意思!

标签: ruby-on-rails devise


【解决方案1】:

你可以在这个方法after_update_path_for(resource)更改路径

class Devise::RegistrationsController < DeviseController

  ## other devise stuff

  protected

  def after_update_path_for(resource)
    new_subscription_path
  end

end

如果你有多个设计模型,你可以试试这个

class Devise::RegistrationsController < DeviseController

  ## other devise stuff

  protected

  def after_update_path_for(resource)
    if resource.is_a?(DeviseModel1)
     new_subscription_path
    else
     other_path
    end
  end

end

或者您可以将它们放入application_controller.rb

【讨论】:

  • 我尝试了第一个建议,因为没有多个设计模型,但它仍然重定向到主页?
【解决方案2】:

您需要为此覆盖设计注册控制器的更新操作。

class Devise::RegistrationsController < DeviseController

  def update
    set_flash_message :notice, :"message here" if is_flashing_format?
    session[:user_return_to] = new_subscription_path
    super  
  end
end

在您的路线中:

devise_for :users, :controllers => {:registrations => 'devise/registrations'}

应该有另一个控制器app/controllers/devise/registrations。上面的控制器代码。

如果您想制作自定义消息,那么它来自config/locales/en.yml:

en:
  devise:
    registrations:
      destroyed: "my custom message." 
      updated: "my custom message"

【讨论】:

  • 我如何生成设计注册控制器?我一个都没有。我的应用程序控制器中的所有设计都与我相关?
  • 您可以直接添加到您的应用中。在我指定的路径中
  • 谢谢我按照上面的方法做了,但是在用户点击他们的编辑个人资料页面上的更新后,它仍然会返回主页?
  • 您将注册控制器放在哪里?
  • 应用程序/控制器/设计/注册
猜你喜欢
  • 2013-01-09
  • 2011-05-05
  • 1970-01-01
  • 2018-07-26
  • 1970-01-01
  • 2023-03-10
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多