轨。创建用户控制器。我的编辑方法没有显示ID

时间:2015-06-28 00:29:30

标签: ruby-on-rails routes filepath controllers

我正在创建一个应用。我创建了一个用户控制器,并成功创建了New和Create方法。运行rails控制台,我可以显示我创建的任何ID。我不明白当我尝试从users / index.html页面编辑时,我没有被定向到/ users / id / edit它只是被定向到/ users

检查传递的参数,当我点击该特定用户的编辑时,它确实显示了正确的ID。

路线:

Prefix Verb   URI Pattern               Controller#Action
  users_index GET    /users/index(.:format)    users#index
welcome_index GET    /welcome/index(.:format)  welcome#index
       macros GET    /macros(.:format)         welcome#macros
         faqs GET    /faqs(.:format)           welcome#faqs
         root GET    /                         welcome#index
        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
              PATCH  /users/:id(.:format)      users#update
              PUT    /users/:id(.:format)      users#update
              DELETE /users/:id(.:format)      users#destroy

UsersController:

class UsersController < ApplicationController
  def new

    @user = User.new
  end
  def create
    @user = User.new(set_user_params)
    if @user.save 
      redirect_to users_path
    else
    end
  end
  def index
    @users = User.all
  end
  def edit
    raise params.inspect
    @user = User.find(params[:id])
  end


  private 

    def set_user_params
      params.require(:user).permit(:name, :email, :team, :password)
    end
end

的index.html

<div> <%= link_to "Create New Agent", new_user_path %></div>

<% @users.each.with_index(1) do |user, index| %>
    <div class="list_of_agents">
    <%= user.name %><br>
    <%= user.team %><br>
    <%= user.id %><br>
    <%= link_to "Edit", edit_user_path(user.id) %><br>
    </div>
<% end %>

1 个答案:

答案 0 :(得分:1)

请将edit网址更改为

<%= link_to "Edit", edit_user_path(user) %>