1
我正在創建一個應用程序。我創建了一個用戶控制器,併成功創建了New和Create方法。運行rails控制檯,我可以調出我創建的任何ID。我不明白當我試圖從users/index.html頁面進行編輯時我沒有被引導到/ users/id/edit它只是被引導到/用戶Rails。創建用戶控制器。我的編輯方法不顯示ID
檢查傳遞的參數,它確實在單擊該特定用戶的編輯時顯示正確的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 %>
你是不是說在'edit'動作中調用'raise params.inspect'時,'params [:id]'沒有值?或者'編輯'行動永遠不會到達? –