重定向回索引

时间:2014-08-25 06:16:11

标签: ruby-on-rails devise

我正在构建一个使用设计的网络应用。注册时使用:email:密码和:密码确认,新用户将被重定向到第二个表单,在那里他们将输入社交信息(例如爱好,年龄,位置等等),此时表单只包含字段名字和姓氏。

提交第二个表单后,应该将用户重定向回用户#index页面,这也是登录用户被重定向到的位置。我刚刚创建了第二个表单,提交后我收到以下错误:

No route matches [POST] "/users/user/edit_profile"

以下是相关文件。我很新,有没有人对此有任何想法?

的routes.rb

Appname::Application.routes.draw do

  devise_for :users, :controllers => { :registrations => "registrations" }

  devise_scope :user do
    get 'register', to: 'devise/registrations#new'
    get 'login',    to: 'devise/sessions#new',     as: :login
    get 'logout',   to: 'devise/sessions#destroy', as: :logout
  end

  resources :users do
    member do
      get 'edit_profile'
    end
  end
  root to: "home#index"
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get' 
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/legal',   to: 'static_pages#legal',   via: 'get'

end

users_controller.rb

class UsersController < ApplicationController
  before_filter :authenticate_user!
    def index
      @users = User.all
    end

    def show
      @user = User.find(params[:id])
    end

    def new
    end

    def create
    end

    def edit
    end

    def update
      @user = User.find(params[:id])
      @user.update!(user_params)
      redirect_to @user
    end

    def destroy
    end

    def edit_profile_user
      @user = User.find(params[:id])
      redirect_to @user
    end

    def user_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :hobbies)
    end

end

edit_profile.html.erb

<h2>Tell us about yourself</h2>

<%= form_for(resource, as: resource_name, url: edit_profile_user_path(resource_name)) do |f| %>

  <%= f.label :first_name %><br />
  <%= f.text_field :first_name, autofocus: true %>

  <%= f.label :last_name %><br />
  <%= f.text_field :last_name %>

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

这是我的佣金路线:

new_user_session GET    /users/sign_in(.:format)          devise/sessions#new
            user_session POST   /users/sign_in(.:format)          devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
           user_password POST   /users/password(.:format)         devise/passwords#create
       new_user_password GET    /users/password/new(.:format)     devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)    devise/passwords#edit
                         PATCH  /users/password(.:format)         devise/passwords#update
                         PUT    /users/password(.:format)         devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)           registrations#cancel
       user_registration POST   /users(.:format)                  registrations#create
   new_user_registration GET    /users/sign_up(.:format)          registrations#new
  edit_user_registration GET    /users/edit(.:format)             registrations#edit
                         PATCH  /users(.:format)                  registrations#update
                         PUT    /users(.:format)                  registrations#update
                         DELETE /users(.:format)                  registrations#destroy
                register GET    /register(.:format)               devise/registrations#new
                   login GET    /login(.:format)                  devise/sessions#new
                  logout GET    /logout(.:format)                 devise/sessions#destroy
       edit_profile_user GET    /users/:id/edit_profile(.:format) users#edit_profile
                   users GET    /users(.:format)                  users#index
                         POST   /users(.:format)                  users#create
                new_user GET    /users/new(.:format)              users#new
               edit_user GET    /users/:id/edit(.:format)         users#edit
                    user GET    /users/:id(.:format)              users#show
                         PATCH  /users/:id(.:format)              users#update
                         PUT    /users/:id(.:format)              users#update
                         DELETE /users/:id(.:format)              users#destroy
                    root GET    /                                 home#index
                   about GET    /about(.:format)                  static_pages#about
                 contact GET    /contact(.:format)                static_pages#contact
                    help GET    /help(.:format)                   static_pages#help
                   legal GET    /legal(.:format)                  static_pages#legal

2 个答案:

答案 0 :(得分:2)

变化:

resources :users do
  member do
    post 'edit_profile'
  end
end

答案 1 :(得分:0)

改为使用edit_profile_user_path(@user)

在Devise登录页面中,resource_name的值只是一个字符串"user" ...您希望指向实际用户的页面,因为您需要一个实际的用户实例。 ...对于Devise的登录页面是resource中存储的内容...但是在您登录后,它存储在current_user中。

但是,您的代码似乎表明您将用户置于@user - 这就是我使用的内容。