0

当我在创建报价时遇到错误时,它会呈现它刚刚所在的同一页面并显示错误。不幸的是,两个输入是字符串的下拉菜单,当刷新发生时它们会消失。

我看过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" %>
4

1 回答 1

0

@colors并且@types只设置在new动作中,而不是create动作中。渲染模板不会自动调用控制器中的action方法;它直接进入视图。

一种可能的解决方案是为这些列表定义辅助方法:

# app/helpers/quote_helper.rb
module QuoteHelper
  def possible_types
    ["T-Shirt", "Hoodie", "Sweatpants"]
  end
end

在您看来:

<%= f.input :make, collection: possible_types, label: 'Thread Type' %>
于 2013-09-12T00:38:46.880 回答