微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ActionController::UrlGenerationError in ModelController#create

如何解决ActionController::UrlGenerationError in ModelController#create

使用名为 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]

我会让代码自己说话

shootings_controller.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 }
      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

routes.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 %>

解决方法

您必须更改以下网址,它才能正常工作

int main()
{
    static char *tests[] = {
        "0","-1","-210","-211","-222","1","210","211","222","5400",NULL,};

    int i,passed = 0;
    for (i = 0; tests[i]; i++) {

        char *test = tests[i];
        int expected,actual;

        P("Testing '%s' conversion\n",test);

        /* expected,decoded with system routines */
        if (sscanf(test,"%i",&expected) != 1) {
            P("problem scanning %s\n",test);
            continue;
        }

        /* actual,decoded with our function */
        actual = decode(test,10);
        char *operator = actual == expected ? "==" : "!=";
        char buff[100]; /* temporary variable to hold the
                         * converted value to binary */
        P("Test result: actual(%i/0b%s)\n",actual,to_binary(actual,buff,sizeof buff));
        P("        %s expected(%i/0b%s)\n",operator,expected,to_binary(expected,sizeof buff));
        if (actual == expected)
            passed++;
    }
    P("passed %d/%d tests\n",passed,i);
}

或轨道方式

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

但我个人认为它应该像上面一样工作,但有些方法不起作用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。