使用Devise和Devise_Invitable编辑用户信息

时间:2013-10-01 14:09:35

标签: ruby-on-rails devise form-for devise-invitable

在过去一周左右的时间里,我正在努力解决这个问题,而且我已经尝试了所有我能想到的东西,所以我需要你的帮助..!我正在使用设计并设计出引人入胜的

我创建了一个页面来编辑用户信息,如用户名,名字,姓氏......

# /controllers/settings_controllers.rb 
class SettingsController < ApplicationController   
  def info
    @user = current_user   
  end 
end

# /controllers/users_controllers.rb 
class UsersController < Devise::SessionsController   
  def update
    @user = User.find(current_user.id)
    if @user.update_attributes(user_params)
      redirect_to :back
    end
  end
end

# /views/settings/info.html.erb 
<%= form_for(@user) do |f| %> 
<%= render 'shared/error_messages' %> 
<%= f.label :username %> 
<%= f.text_field :username %> 
<%= f.label :firstname %> 
<%= f.text_field :firstname %> 
.... 
<% button_value = "Update" %> 
<% end %>

我的路线:

  devise_for :users ,:controllers => { :invitations => 'users/invitations' }
  resources :users, only: [:edit, :update]
  devise_for :users, :skip => [:registrations]                                          
    as :user do
      get 'user/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'    
      put 'user' => 'devise/registrations#update', :as => 'user_registration'            
    end

  devise_scope :user do
    authenticated :user do
      root :to => 'aggregator#index'
    end
    unauthenticated :user do
      root :to => 'devise/sessions#new'
    end

    get "users/new" => "users#new"
    get "users/:id" => "users#show"
  end
  match 'settings/info' => 'settings#info', :as => 'info'

当我尝试更新表单时,出现以下错误(我的用户ID为1):

Could not find devise mapping for path "/users/1"

修改

所以我把resources :users, only: [:edit, :update]放在devise_for :users之后,就像@coletrain建议的那样,错误就消失了。但是当我想要重定向到/users/1时,它会重定向到我的个人资料/settings/info,更重要的是,它不会更新我的信息... 我的猜测是没有达到users_controller中的更新方法。

2 个答案:

答案 0 :(得分:1)

在routes.rb中:  在put "users/:id" => "users#update"块内添加devise_scope :user do ... end

此外:  在user_controller更新方法中,将@user.update_attributes(user_params)更改为@user.update_attributes(params["user"])

答案 1 :(得分:1)

我遇到了同样的问题。我认为最简单的解决方案就是:默认使用设计为您提供的。

<强>路线:

devise_scope :user do

  get "account", to: "devise/registrations#edit"
  patch "account", to: "devise/registrations#update"
  put "account", to: "devise/registrations#update"
  delete "account", to: "devise/registrations#destroy"

end

/ views / devise / registrations / edit.html.erb #generate bygene 替换这样的路径:

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

到此(因为我在此示例中将路线命名为“account”)

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

请注意,您还必须删除resource_name 。否则,在您提交更改后会出现路由问题