Rails - 更新用户返回“无法找到具有id = edit”的用户

时间:2013-05-02 07:22:07

标签: ruby-on-rails ruby-on-rails-3 devise

views/users/index.html.erb我有:

<% @users.each do |user| %>
  <% if user.teacher == current_user.teacher || current_user.role == "admin" %>
  <tr>
    <td><%= user.username %></td>
    <td><%= user.email %></td>
    <td><%= user.teacher %></td>
    <td><%= user.role %></td>
    <td><%= user.admin %></td>
    <td><%= link_to 'Problems', student_problems_path(user_email: user.email) %></td>
    <td><%= link_to 'Edit', edit_user_registration_path(user) %></td>
    <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
  <% end %>
<% end %>
</table>

当您点击Edit时,您会链接到views/users/edit.html.erb,如下所示:

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

<%= form_for(resource, :as => resource_name, :url => 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, :autofocus => true %></div>

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

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

问题是,当我点击Update并实际尝试更新模型时,我得到了

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

以下是update中的editUsersController操作:

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

    redirect_to(user_path)
end

而且,如果有帮助,这是我的路线:

                  root        /                              problems#new
        feedback_index POST   /feedback(.:format)            feedback#create
          new_feedback GET    /feedback/new(.:format)        feedback#new
      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
                       PUT    /users/password(.:format)      devise/passwords#update
edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
     user_registration PUT    /users(.:format)               devise/registrations#update
                 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
                       PUT    /users/:id(.:format)           users#update
                       DELETE /users/:id(.:format)           users#destroy
              problems GET    /problems(.:format)            problems#index
                       POST   /problems(.:format)            problems#create
           new_problem GET    /problems/new(.:format)        problems#new
          edit_problem GET    /problems/:id/edit(.:format)   problems#edit
               problem GET    /problems/:id(.:format)        problems#show
                       PUT    /problems/:id(.:format)        problems#update
                       DELETE /problems/:id(.:format)        problems#destroy
            help_about GET    /help/about(.:format)          help#about
          problems_new GET    /problems/new(.:format)        problems#new
                  data        /data(.:format)                problems#data
      student_problems        /student_problems(.:format)    users#student_problems

感谢所有为我着想这个问题的人,最近它已经出现了很多。

4 个答案:

答案 0 :(得分:1)

你说的是错误的道路。在您的视图中更改

edit_user_registration_path(user)

edit_user_path(user)

答案 1 :(得分:0)

Nick是正确的,您需要使用edit_user_path,它将您引导至Users#edit,而不是Devise注册控制器。

您收到的错误消息表明缺少edit_user_path帮助程序中所需的id参数,这很可能意味着@users中的某个用户未保存(其id为nil)。要调试在视图中尝试某些内容,例如:

<% @users.each do |user| %>
   USER ID = <%= user.id %>
<% end %>

您可能会发现其中一个值为空白。

答案 2 :(得分:0)

是的,尼克是对的。要编辑用户的个人资料,不需要用户edit_user_registration_path(user)。 在设计的帮助下创建新用户时,可能会使用此路径。

在这里,您必须确保使用您的网址传递ID以更新用户。它会是这样的

edit_user_path(@user.id)

{:controller=> 'user', :action => 'edit', :id => @user.id}

答案 3 :(得分:0)

在routes.rb中,您可能会以错误的顺序放置这两行:

Schema::table('users', function (Blueprint $table) { // if (!Schema::hasColumn('users', 'fb_id')) { // $table->string('fb_id')->default(''); } });

然后调用mysite / users / edit将被路由到id为'edit'的用户控制器

它应该是: #routes.rb resources :users devise_for :users