【问题标题】:rails redirect_to after database update数据库更新后的rails redirect_to
【发布时间】:2013-10-01 22:48:47
【问题描述】:

你好,我有一个表格可以更改当前语言。它与 users_controller 中的更新操作相关联,如下所示:

<%= form_for current_user do |f| %>
 <%= f.select :locale, [['En', 'en'], ['Pt', 'pt']] %>
 <%= f.submit %>
<% end %>


class UsersController < ApplicationController
 def update  
  @user.update(user_params)
  I18n.locale=@user.locale
  redirect_to root_path
 end

 def user_params
  params.require(:user).permit(:locale)
 end
end

更新后,我不想重定向到 root_path,但我想重定向到:返回,但使用与更新一致的语言环境参数集。 我不知道,你能帮帮我吗?

【问题讨论】:

    标签: ruby-on-rails redirect params


    【解决方案1】:

    试试这个:

    class ApplicationController < ActionController::Base
    
      before_filter :set_locale
    
      private
    
      def set_locale
        I18n.locale = current_user ? current_user.locale : 'en'
      end
    end
    

    那么你的更新操作就是:

     def update  
       @user.update(user_params)
       redirect_to :back
     end
    

    【讨论】:

    • 不起作用,因为 redirect_to :back 与 redirect_to request.referrer 相同。在 request.referrer 中有请求与旧语言环境参数的链接。 Redirect_to :back 没问题,但我需要将它附加到新的语言环境参数。
    • 如果您希望您的用户在配置文件中设置他们的语言环境,我认为没有必要发送语言环境参数。您能否更新您的问题并显示重定向后如何使用此语言环境参数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多