0

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`

以下是 中的updateedit动作UsersController

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

4 回答 4

1

你叫错了路。在你看来改变

edit_user_registration_path(user)

edit_user_path(user)
于 2013-05-02T07:52:36.733 回答
0

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

在这里,您必须确保将 id 与您的 url 一起传递以更新用户。会是这样的

edit_user_path(@user.id)

或者

{:controller=> 'user', :action => 'edit', :id => @user.id}
于 2013-05-04T07:33:26.407 回答
0

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

您收到的错误消息表明 edit_user_path 帮助程序中所需的 id 参数丢失,这很可能意味着您的 @users 中的一个用户未保存(其 id 为 nil)。要调试,请尝试在您的视图中执行以下操作:

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

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

于 2013-05-02T18:51:42.573 回答
0

在 routes.rb 中,您可能将这两行按错误的顺序排列:

#routes.rb resources :users devise_for :users

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

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

于 2016-02-18T05:04:11.137 回答