在过去的一周左右,我一直在努力解决这个问题,我已经尝试了我所能想到的一切,所以我需要你的帮助。我用的是设计和设计
我创建了一个页面来编辑用户信息,比如用户名,名字,姓氏.
# /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和error建议的那样。但是当我想重定向到/settings/info时,它会重定向到我的配置文件/settings/info,更重要的是,它不会更新我的信息.我的猜测是,users_controller中的更新方法没有实现。
发布于 2013-10-01 19:52:48
在routes.rb:在devise_scope :user do ... end块中添加put "users/:id" => "users#update"。
另外:在user_controller更新方法中,将@user.update_attributes(user_params)更改为@user.update_attributes(params["user"])
发布于 2014-05-26 16:51:21
我也有同样的问题。我认为最简单的解决方案是:默认情况下,只需使用设计给您的东西。
路由:
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 #替换如下路径:
<%= 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太。否则,在提交更改后会出现路由问题。
https://stackoverflow.com/questions/19118430
复制相似问题