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

标签 ruby-on-rails activerecord

每当我单击表单中的更新按钮时,它都会重定向到 /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/

相关文章:

ruby-on-rails - 如何从 Rails 控制台列出 postgres 中的模式?

php - 从另一个表中选择一列用作选择键后,Mysql 连接四个表

ruby-on-rails - 计算月份统计

javascript - 数组或迭代器中的每个 child 都应该有一个唯一的 "key" Prop (使用 Rails 和 React)

ruby-on-rails - 在 Rails 中实现通知

ruby-on-rails - 如何使用单个表单在 ActiveRecord 中保存嵌套资源(Ruby on Rails 5)

PHP Activerecord - 连接同一张表

ruby-on-rails - 如何使用 i18n 本地化 Stimulus JS 值?

ruby-on-rails - 更长的文件名与 git merge

ruby-on-rails - RSPEC 测试名称错误 - 未定义的局部变量或方法