我突然发生了这个奇怪的错误,以前我能够将数据从一种形式提交到另一种形式......我无法弄清楚出了什么问题。任何帮助将不胜感激。
这是视图
<div class="choose-occasion">
<div class="col-md-12">
<center>
<img class="steps-img" src="/assets/choose-occasion/step-1.png">
</center>
</div>
<div class="col-md-12">
<h3 class="heading-text">choose an occasion</h3>
</div>
<div class="container">
<%= simple_form_for @order, url: wizard_path,:method => :put do |f| %>
<center>
<%= f.collection_select :occasion_id, Occasion.all, :id, :name, {},{:class=> 'selectpicker btn-group'} %>
</center>
<input type="hidden" value="<%= spree_current_user.id %>" name="studio_order[user_id]" >
<center>
<%= button_tag(type: "submit", class: "btn btn-primary continue-btn") do %>
Continue
<%= image_tag("choose-occasion/greater.png", class: "gmark") %>
<% end %>
</center>
<% end %>
</div>
</div>
这是控制器..
class OrderDetailsController < ApplicationController
include Wicked::Wizard
steps :choose_occasion, :choose_style, :number_of_people, :choose_package, :upload_images, :instructions_artist
def show
byebug
@order = StudioOrder.new
render_wizard
end
def update
byebug
if StudioOrder.find_by(user_id: spree_current_user.id)
@order = StudioOrder.find_by(user_id: spree_current_user.id)
@order.update_attributes(getparams)
end
render_wizard @order
end
def getparams
params.require(:studio_order).permit(:user_id, :occasion_id, :style_id,:initial_price, :number_of_people,
:artwork_size, :package_id, :instructions)
end
end
以下是
中的参数{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"A64h4cyTdOuZji/2zSvr7K/WE3SZt2UMMP8bitsdY6c5Z2EA02qlJx/SKzE7yZT4lRMFPi8sDpJzSOi9m9NkLQ==", "studio_order"=>{"occasion_id"=>"1", "user_id"=>"2"}, "button"=>"", "controller"=>"order_details", "action"=>"update", "id"=>"choose_occasion"}
答案 0 :(得分:0)
错误'model_name' for nil:NilClass for simple form
表示您的@order
变量中没有任何数据。
因为form_for
/ simple_form_for
从传递的对象中推断出所有数据,所以没有数据会阻止它填充其选项(model_name
等)。
我能看到的唯一问题(我不会使用Wicked
)是......
#orderdetails#update
if StudioOrder.find_by(user_id: spree_current_user.id) #-> @order only set if user exists
@order = StudioOrder.find_by(user_id: spree_current_user.id)
@order.update_attributes(getparams)
end
这里的传统模式是......
def update
@order = StudioOrder.find_by(user_id: spree_current_user.id)
<<do something>>if @order.update getparams
end
我推测因为您正在使用Wicked
,如果您没有@order
,重新呈现表单可能无法正常工作变量到位。要修复它,我会删除if
逻辑:
#orderdetails#update
def update
@order = StudioOrder.find_by(user_id: spree_current_user.id)
@order.update_attributes(getparams)
render_wizard @order
end
如果您确实需要授权spree_current_user
,则需要使用逻辑来体现整个流程,而不仅仅是@order
声明。