0

在过去一周左右的时间里,我一直在努力解决这个问题,我已经尝试了我能想到的一切,所以我需要你的帮助..!我正在使用设计和设计邀请

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

# /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 中的更新方法。

4

2 回答 2

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"])

于 2013-10-01T19:52:48.120 回答
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 #devise生成的替换路径如下:

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

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

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

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

于 2014-05-26T16:51:21.910 回答