Friends#new中的Ruby Rails ActionController::UrlGenerationError缺少必需的键:[:标识]

lxkprmvk  于 2023-02-12  发布在  Ruby
关注(0)|答案(1)|浏览(113)

当我分别将rails从5.2升级到6.1版,ruby从2.5升级到2.7版,试图在rails 6.1中访问我的新模型的新页面时,我看到了下面的错误。
No route matches {:action=>"show", :controller=>"friends", :format=>nil, :id=>#<Friend id: nil, first_name: nil, last_name: nil, created_at: nil, updated_at: nil>}, possible unmatched constraints: [:id]
这是我朋友的模型

class Friend < ApplicationRecord
end

这里是朋友控制器

class FriendsController < ApplicationController
  before_action :set_friend, only: [:show, :edit, :update, :destroy]

  # GET /friends
  def index
    @friends = Friend.all
  end

  # GET /friends/1
  def show
  end

  # GET /friends/new
  def new
    @friend = Friend.create
  end

  # GET /friends/1/edit
  def edit
  end

  # POST /friends
  def create
    @friend = Friend.new(friend_params)

    if @friend.save
      redirect_to @friend, notice: 'Friend was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /friends/1
  def update
    if @friend.update(friend_params)
      redirect_to @friend, notice: 'Friend was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /friends/1
  def destroy
    @friend.destroy
    redirect_to friends_url, notice: 'Friend was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_friend
      @friend = Friend.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def friend_params
      params.require(:friend).permit(:first_name, :last_name)
    end
end

这是抛出错误的新朋友视图

<h1>New Friend</h1>

<%= render 'form', friend: @friend %>

<%= link_to 'Back', friends_path %>

这里是视图引用的_form

<%= form_with(model: friend) do |form| %>
  <% if friend.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(friend.errors.count, "error") %> prohibited this friend from being saved:</h2>

      <ul>
        <% friend.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :first_name %>
    <%= form.text_field :first_name %>
  </div>

  <div class="field">
    <%= form.label :last_name %>
    <%= form.text_field :last_name %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

由于某种原因,/friends/new调用show,由于show需要一个:id,调用失败并显示错误,这可能发生在我最近升级的rails和ruby上
这是我的路由文件

RailsRoot::Application.routes.draw do
  resources :friends
  root :to => "accounts#index"

  resources :accounts
end

下面是我运行rails routes时看到结果

Prefix Verb   URI Pattern                                                                                       Controller#Action
                                 friends GET    /friends(.:format)                                                                                friends#index
                                         POST   /friends(.:format)                                                                                friends#create
                              new_friend GET    /friends/new(.:format)                                                                            friends#new
                             edit_friend GET    /friends/:id/edit(.:format)                                                                       friends#edit
                                  friend GET    /friends/:id(.:format)                                                                            friends#show
                                         PATCH  /friends/:id(.:format)                                                                            friends#update
                                         PUT    /friends/:id(.:format)                                                                            friends#update
                                         DELETE /friends/:id(.:format)                                                                            friends#destroy
                                    root GET    /                                                                                                 accounts#index
                                accounts GET    /accounts(.:format)                                                                               accounts#index
                                         POST   /accounts(.:format)                                                                               accounts#create
                             new_account GET    /accounts/new(.:format)                                                                           accounts#new
                            edit_account GET    /accounts/:id/edit(.:format)                                                                      accounts#edit
                                 account GET    /accounts/:id(.:format)                                                                           accounts#show
                                         PATCH  /accounts/:id(.:format)                                                                           accounts#update
                                         PUT    /accounts/:id(.:format)                                                                           accounts#update
                                         DELETE /accounts/:id(.:format)                                                                           accounts#destroy

新视图的问题发生在我所有的模型和视图中,而不仅仅是朋友控制器(其他控制器是帐户)。
以下是指向错误的完整堆栈跟踪的链接:https://pastecode.io/s/6g8itf96
我试图打开我的模型的新视图,并期待一个空的表单,我可以填写字段并保存。我怀疑/friends/new调用show时没有:id,这是问题的原因。在我将rails升级到6.1之前,这可能不会发生

x3naxklr

x3naxklr1#

我认为主要的问题在于控制器中的#new方法,因为您是通过使用.create方法将其保存在DB中来创建一个新朋友的,而不是为表单创建一个空对象
你把它改成

# GET /friends/new
  def new
    @friend = Friend.new
  end

相关问题