首页 > 解决方案 > ModelController#create 中的 ActionController::UrlGenerationError

问题描述

使用名为 Shootings 的模型在 Rails 应用程序中工作。它有几个字段,所以我使用 Wicked gem 分几个步骤收集它们。

我能够正确创建射击记录,并且如果我手动键入路径,我能够正确启动 Wicked 向导。但是,我无法在使用向导的第一步创建拍摄后设置重定向。

一旦我在创建拍摄后尝试重定向到向导的第一步,就会在 build_controller.rb 中收到此错误

No route matches {:action=>"show", :controller=>"shootings/build", :shooting_id=>#<Shooting id: 100, title: "cwe AVER", created_at: "2021-02-13 20:01:26.212909000 +0000", updated_at: "2021-02-13 20:01:26.212909000 +0000", user_id: 1, typeshooting: nil, numberimages: nil, proservices: nil, goals: nil, status: nil>}, missing required keys: [:id]

我会让代码自己说话

枪击控制器.rb

class ShootingsController < ApplicationController
  before_action :set_shooting, only: %i[ show edit update destroy ]

  # GET /shootings/new
  def new
    @shooting = Shooting.new
  end

  # POST /shootings or /shootings.json
  def create
    @shooting = Shooting.new(shooting_params)

    respond_to do |format|
      if @shooting.save
        format.html { redirect_to shooting_build_path(@shooting), notice: "Shooting was successfully created." }
        format.json { render :show, status: :created, location: @shooting }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @shooting.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /shootings/1 or /shootings/1.json
  def update
    respond_to do |format|
      if @shooting.update(shooting_params)
        format.html { redirect_to @shooting, notice: "Shooting was successfully updated." }
        format.json { render :show, status: :ok, location: @shooting }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @shooting.errors, status: :unprocessable_entity }
      end
    end
  end

end

build_controler.rb(在控制器 > 枪击中)

class Shootings::BuildController < ApplicationController
  include Wicked::Wizard

  steps :add_typeshooting, :add_numberimages, :add_proservices, :add_goals

  def show
    @shooting = Shooting.find(params[:shooting_id])
    render_wizard
  end

  def update
    @shooting = Shooting.find(params[:shooting_id])
    params[:shooting][:status] = 'active' if step == steps.last
    @shooting.update(shooting_params)
    render_wizard @shooting
  end

  def create
    @shooting = Shooting.create
    redirect_to wizard_path(steps.first, shooting_id: @shooting.id)
  end

  private

  def redirect_to_finish_wizard options = nil, params =  {}
    redirect_to root_url, notice: 'Thanks for your shooting'
  end
  
  # Only allow a list of trusted parameters through.
  def shooting_params
    params.require(:shooting).permit(:title, :user_id, :typeshooting, :numberimages, :proservices, :goals)
  end

  def set_shooting
    @shooting = Shooting.find(params[:id])
  end

end

路线.rb

Rails.application.routes.draw do
  resources :users
  resources :shootings do
    resources :build, controller: 'shootings/build'
  end
  resources :photos
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root "shootings#index"
end

和铁路路线

shooting_build_index GET    /shootings/:shooting_id/build(.:format)                                                           shootings/build#index
                                         POST   /shootings/:shooting_id/build(.:format)                                                           shootings/build#create
                      new_shooting_build GET    /shootings/:shooting_id/build/new(.:format)                                                       shootings/build#new
                     edit_shooting_build GET    /shootings/:shooting_id/build/:id/edit(.:format)                                                  shootings/build#edit
                          shooting_build GET    /shootings/:shooting_id/build/:id(.:format)                                                       shootings/build#show
                                         PATCH  /shootings/:shooting_id/build/:id(.:format)                                                       shootings/build#update
                                         PUT    /shootings/:shooting_id/build/:id(.:format)                                                       shootings/build#update
                                         DELETE /shootings/:shooting_id/build/:id(.:format)                                                       shootings/build#destroy
                               shootings GET    /shootings(.:format)                                                                              shootings#index
                                         POST   /shootings(.:format)                                                                              shootings#create
                            new_shooting GET    /shootings/new(.:format)                                                                          shootings#new
                           edit_shooting GET    /shootings/:id/edit(.:format)                                                                     shootings#edit
                                shooting GET    /shootings/:id(.:format)                                                                          shootings#show
                                         PATCH  /shootings/:id(.:format)                                                                          shootings#update
                                         PUT    /shootings/:id(.:format)                                                                          shootings#update
                                         DELETE /shootings/:id(.:format)                                                                          shootings#destroy

枪击案

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

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

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

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

标签: ruby-on-railsruby-on-rails-6wicked-gem

解决方案


您必须更改以下网址,它正在工作

format.html {
  flash.now[:notice] = 'Shooting was successfully created.'
  redirect_to shooting_build_index_path(@shooting) 
 }

或轨道方式

format.html do
  flash.now[:notice] = 'Shooting was successfully created.'
  redirect_to shooting_build_index_path(@shooting) 
 end

但我个人认为它应该像上面那样工作,但有些它不工作。


推荐阅读