【问题标题】:Rails 3: Strange devise redirect errorRails 3:奇怪的设计重定向错误
【发布时间】:2012-09-13 01:27:17
【问题描述】:

所以,我遇到了一个有趣的问题。我正在尝试覆盖并将设计 sign_up 和 sign_in 请求重定向到其相应的个人资料页面,但我遇到了错误。配置文件和 URL 可以通过控制台、link_to 或通过输入来访问。由于某种原因,设计方法不会路由到它。我不确定为什么。无论如何,为所有贡献或解决的人投票。谢谢!

路线:

  root :to => 'pages#index'
  get "pages/index"

  devise_for :users, :path => 'accounts', :controllers => { :registrations => "registrations" }

  get 'users/:id/profile' => 'profiles#show', :as => 'current_profile'
  get 'users/:id/profile/edit' => 'profiles#edit', :as => 'edit_current_profile'
  put 'users/:id/profile' => 'profiles#update'

  resources :users do
    resources :profiles 
  end

注册控制器:

class RegistrationsController < Devise::RegistrationsController

  protected

  def after_sign_up_path_for(resource)
     edit_current_profile_path(resource)
  end

  def after_sign_in_path_for(resource)
     current_profile_path(resource)
  end

end

配置文件控制器

  def show
    @user = current_user
    @profile = @user.profile

    respond_to do |format|
      format.html 
    end
  end

错误信息:

Routing Error

No route matches {:controller=>"profiles", :action=>"show"}
Try running rake routes for more information on available routes.

查看:edit.html.erb

<%= form_for([@profile.user, @profile]) do |f| %>
  <% if @profile.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@profile.errors.count, "error") %> prohibited this profile from being saved:</h2>

      <ul>
      <% @profile.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :real_name %><br />
    <%= f.text_field :real_name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise routes mongoid


    【解决方案1】:

    你应该给你的路由助手一个属性。试试看

    def after_sign_up_path_for(resource)
       edit_current_profile_path(resource)
    end
    
    def after_sign_in_path_for(resource)
       current_profile_path(resource) 
    end
    

    【讨论】:

    • 感谢您的回复。我现在在那里添加了属性,但仍然是同样的错误
    • 可能是你认为的路由助手造成的。您可以将个人资料编辑页面添加到您的问题中吗?
    • 好的。刚刚更新了。这很奇怪,因为如果我手动访问 URL,我可以更新不同用户的个人资料。
    • 在这里没有发现任何犯罪行为,抱歉。仍然认为是由无效的路由助手引起的错误。类似于profile_path 而不是user_profile_path
    • 所以我改变了 get 匹配,它停止给我错误!感谢您的反馈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2013-03-29
    相关资源
    最近更新 更多