当我在创建报价时遇到错误时,它会呈现它刚刚所在的同一页面并显示错误。不幸的是,两个输入是字符串的下拉菜单,当刷新发生时它们会消失。
我看过Rail 3: instance variable not available after redirection讨论会话,这看起来可能是正确的方法,但我不确定。任何帮助,将不胜感激。
报价控制器
def new
@quote = Quote.new
@quote.items.build
@types = ["T-Shirt", "Hoodie", "Sweatpants"]
@colors = ["White", "Black", "Red", "Blue", "Green"]
end
def create
@quote = Quote.new(params[:quote])
respond_to do |format|
if @quote.save
format.html { redirect_to root_url }
flash[:success] = "Your quote is being approved. You will recieve an email shortly!"
format.json { render json: @quote, status: :created, location: @quote }
else
format.html { render :action => 'new' }
format.json { render :json => @quote.errors, :status => :unprocessable_entry }
flash[:error] = "Quote failed to create! Try again!"
end
end
end
形成部分
<!-- item form -->
<%= f.input :make, collection: @types, label: 'Thread Type' %>
<%= f.input :amount, label: 'How Many' %>
<%= f.input :color, collection: @colors %>
<!-- nested form for creating a design of an item -->
<%= f.simple_fields_for :designs, :html => { :multipart => true } do |designform| %>
<%= render "customdesign", g: designform %>
<% end %>
<!-- add/remove another design -->
<%= f.link_to_add "Add Design", :designs %>
<%= f.input :note, :input_html => { :cols => 50, :rows => 3 }, label: 'Special Notes or Requests' %>
<%= f.link_to_remove "Remove" %>