gpt4 book ai didi

ruby-on-rails - 邪恶的 gem 在更新操作中找不到婚礼 ID

转载 作者:行者123 更新时间:2023-12-04 18:26:02 24 4
gpt4 key购买 nike

我正在尝试为巫师实现“Wicked”gem,但我这辈子都无法找出这个错误。已引用Ryan bates railscast #346step by step tutorial by schneems .

我有 2 个 Controller :Weddings 和 Wedding_steps。用户最初创建一个婚礼,在创建操作被重定向到 Wedding_steps Controller (使用 Wicked)以使用附加信息更新婚礼模型。

在第一步weddingdetails中成功检测到wedding_id,但是提交那一步后,出现如下错误:

错误

ActiveRecord::RecordNotFound in WeddingStepsController#update

Couldn't find Wedding without an ID:app/controllers/wedding_steps_controller.rb:11:in `update'

Parameters:

{"utf8"=>"✓","_method"=>"put","authenticity_token"=>"JMd+8gf4rVqOSNMSFrKcD3WxK+X3zvYliSMWqTg0SkE=","wedding"=>{"bridename"=>"","groomname"=>"","weddingdate"=>"","weddingcity"=>"","weddingstate"=>"","url"=>""},"commit"=>"Next","id"=>"wedding_id=11"}

它应该继续下一步 /wedding_steps/eventdetails?wedding_id=11 但给出错误并转到 /wedding_steps/wedding_id=11

还需要注意的是,如果没有适当的更新操作,信息会成功保存并重定向到 Wedding Show 操作。

相关代码如下:

wedding_steps_controller.rb

class WeddingStepsController < ApplicationController
include Wicked::Wizard
steps :weddingdetails, :eventdetails

def show
@wedding = Wedding.find(params[:wedding_id])
render_wizard
end

def update
@wedding = Wedding.find(params[:wedding_id])
@wedding.update_attributes(params[:wedding])
render_wizard @wedding
end

end

weddings_controller.rb

def create
@wedding = current_user.weddings.new(params[:wedding])


respond_to do |format|
if @wedding.save
format.html { redirect_to wedding_steps_path(:id => "weddingdetails", :wedding_id => @wedding.id) }
format.json { render json: @wedding, status: :created, location: @wedding }
else
format.html { render action: "new" }
format.json { render json: @wedding.errors, status: :unprocessable_entity }
end
end
end

第 1 步:wedding_steps/weddingdetails.html.erb

<%= simple_form_for(@wedding, :url => wizard_path(wedding_id: @wedding.id), :method => :put, html: { class: 'form-horizontal'}) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">


<div class="formrow center">
<%= f.input :bridename, placeholder: "The Bride's Name", label: false %>
<h2 class="inline"> &</h2>
<%= f.input :groomname, placeholder: "The Groom's Name", label: false %>
</div>


<div class="formrow center">
<%= f.text_field :weddingdate %>
<!-- OLD STYLE DATE FORMAT <%= f.input :weddingdate, label: "Wedding Date" %> -->
<%= f.input :weddingcity, label: "City" %>
<%= f.input :weddingstate, label: "State" %>
</div>


<div class="formrow center">
<%= f.input :url, placeholder: "i.e. 'johnandkate' ", label: false %>
</div>

</div>


<div class="form-actions center">
<%= f.button :submit, value: "Next" %>
</div>



<% end %>

<%= link_to 'skip', next_wizard_path(wedding_id: @wedding.id) %>

第 2 步:wedding_steps/eventdetails.html.erb

EVENT DETAILS STEP <!--PLACEHOLDER FOR NOW -->

Routes.rb

Jobshop::Application.routes.draw do    
resources :pins
resources :weddings
resources :wedding_steps

get "users/show"

root :to => 'pages#home'
get 'about' => 'pages#about'

devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)

resources :inviterequests


devise_for :views
ActiveAdmin.routes(self)

devise_for :users
ActiveAdmin.routes(self)

match 'users/:id' => 'users#show'


最佳答案

这一行:

<%= simple_form_for(@wedding, :url => wizard_path, :method => :put, html: { class: 'form-horizontal'}) do |f| %>

应该是:

<%= simple_form_for(@wedding, :url => wizard_path(wedding_id: @wedding.id), :method => :put, html: { class: 'form-horizontal'}) do |f| %>

注意 wizard_path(wedding_id: @wedding.id) 当您提交表单时,您应该在日志中看到 parameters = {:wedding_id => some_number}

如果更新操作不起作用,请粘贴 params 的输出。

编辑:

您应该将 ":wedding_id" 作为所需 url 的一部分,除非它具有格式正确的 url,否则甚至无法生成指向该 Controller 的链接。

替换这个

resources :wedding_steps

有了这个

  scope "weddings/:wedding_id" do
resources :wedding_steps
end

所以现在正确的 url 看起来像 weddings/83/wedding_steps/weddingdetails。您的一个或多个 View 助手可能没有正确包含 wedding_id 并且使用这个新约束,您将在 View 中引发错误,但这是一件好事,因为它会告诉您格式错误的链接在哪里。

关于ruby-on-rails - 邪恶的 gem 在更新操作中找不到婚礼 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18175301/

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