gpt4 book ai didi

ruby-on-rails - ActiveRecord::RecordNotFound "Couldn' t 找到 id=new 的用户”

转载 作者:行者123 更新时间:2023-12-02 01:55:15 25 4
gpt4 key购买 nike

每当我单击表单中的更新按钮时,它都会重定向到 /users/new.1 并引发错误 Couldn't find User with id=new。请帮忙!

用户 Controller .rb

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

def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
redirect_to @user
else
render "edit"
end
end

private
def user_params
params.require(:user).permit(:id, :name, :password, :password_confirmation, :email, :password_salt, :password_hash, :phone)
end

edit.html.erb

<%= form_for @user, url: user_path(@user), method: :patch do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %><br />
<%= f.label :mobile %><br />
<%= f.text_field :phone %><br />
<%= f.label :email %><br />
<%= f.text_field :email %><br />
<%= f.label :role %><br />
<%= f.text_field :role %><br />
<p class="button"><%= f.submit "Update"%></p>
<% end %>

路线.rb

  get "logout" => "pages#destroy", :as => "logout"
get 'welcome/index'
get 'users/new', :as => 'user'
root :to => 'welcome#index'
resources :users

最佳答案

从 routes.rb 文件中删除多余的 get 'users/new', :as => 'user' 行。 resources :users 为您处理所有 CRUD 路由。

关于ruby-on-rails - ActiveRecord::RecordNotFound "Couldn' t 找到 id=new 的用户”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20554773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com