2013-05-01 75 views
0

我正在使用Rails 3和Devise进行身份验证。当我尝试更新我current_user,抛出一个异常,指出:Rails - 无法找到用户id =编辑

Couldn't find User with id=edit 
app/controllers/users_controller.rb:49:in `update' 

这里是UsersController updateedit

#UsersController.rb 
def update 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     if @user.update_attributes(params[:user]) 
     format.html { redirect_to @user, notice: 'User was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

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

这里是/views/layouts/_navigation.html.erb

<%= link_to "Home", root_path, :class => 'brand' %> 
<ul class="nav"> 
    <% if user_signed_in? %> 
    <li> 
    <%= link_to('Logout', destroy_user_session_path, :method=>'delete') %> 
    </li> 
    <% else %> 
    <li> 
    <%= link_to('Login', new_user_session_path) %> 
    </li> 
    <% end %> 
    <li> 
    <%= link_to('About', help_about_path) %> 
    </li> 
    <% if user_signed_in? %> 
    <li> 
    <%= link_to('Edit Account', edit_user_path(current_user)) %> 
    </li> 
    <% end %> 
    <% if user_signed_in? and current_user.role == "gen_admin" || current_user.role == "teacher" %> 
    <li> 
    <%= link_to('View Students', users_path) %> 
    </li> 
    <% end %> 
    <% if user_signed_in? and current_user.role == "gen_admin" || current_user.role == "teacher" %> 
    <li> 
    <%= link_to('New User', new_user_path) %> 
    </li> 
    <% end %> 
    <% if user_signed_in? %> 
    <li> 
    <%= link_to('Student Data', data_path) %> 
    </li> 
    <% end %> 
</ul> 

最后,这里是/views/devise/registrations/edit.html.erb

<h2>Edit <%= resource_name.to_s.humanize %></h2> 

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

    <div><%= f.label :email %><br /> 
    <%= f.email_field :email, :autofocus => true %></div> 

    <div><%= f.label :teacher %><br /> 
    <%= f.text_field :teacher %></div> 

    <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> 
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div> 
    <% end %> 

    <div><%= f.label :new_password %> <i>(leave blank if you don't want to change it)</i><br /> 
    <%= f.password_field :password, :autocomplete => "off" %></div> 

    <div><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %></div> 

    <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> 
    <%= f.password_field :current_password %></div> 

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

<%= link_to "Back", :back %> 

为什么用户id =编辑?这里是我的哈希值,如果这能帮助:

{"utf8"=>"✓", 
"_method"=>"put", 
"authenticity_token"=>"7g1WsNCVuNY/5ZTeBUdv97tbdAPacvvDAzBSMGCcuNY=", 
"user"=>{"email"=>"[email protected]", 
"teacher"=>"Demont", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]", 
"current_password"=>"[FILTERED]"}, 
"commit"=>"Update", 
"id"=>"edit", 
"format"=>"user"} 
+0

你可以发布你的编辑动作的代码吗? – 2013-05-01 05:38:29

+0

我加了'edit'动作。这里很晚,所以我会尽量在早上使用每个人的反应来解决这个问题,并标记出最适合我的答案。谢谢大家! – Houdini 2013-05-01 06:38:19

回答

1

尝试使用:

@user = current_user 

东西看起来是错误的路线或编辑表单。尝试使用类似的形式在他们的wiki页面... https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password

但上面的代码应该解决这个问题

+0

您可能是对的,我的路线中可能会有更大的问题。但是现在使用'current_user'而不是'params [:id]'适用于我。感谢其他人,我相信这些也是有效的答案。 – Houdini 2013-05-02 04:41:49

1

最有可能你添加UsersController当我有你的路线冲突。

devise's wiki page解释说。

默认devise的编辑用户路由为/users/edit,但在您的控制器路径中更新的操作是/users/:id。什么是为什么你有“编辑”,而不是user_id

0

使用

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

到表单视图和

@resource = User.find(params[:id]) 

在你的编辑行动。