在show和index操作中Rails select_tag问题

时间:2011-09-22 14:37:14

标签: ruby-on-rails views html-select

我有一个select_tag来从列表中选择项目,这里是代码:

<%= select_tag "Drinks", options_for_select([ "Water", "Soda", "Beer" ], "Water") %>

在提交“水”之后,它不会显示在“饮料”部分的索引和显示视图上。

我做错了什么?

控制器创建和更新方法

def create
    @facture = Facture.new(params[:facture])

    respond_to do |format|
      if @facture.save
        format.html { redirect_to @facture, :notice => 'Facture was successfully created.' }
        format.json { render :json => @facture, :status => :created, :location => @facture }
      else
        format.html { render :action => "new" }
        format.json { render :json => @facture.errors, :status => :unprocessable_entity }
      end
    end
  end


def update
    @facture = Facture.find(params[:id])

    respond_to do |format|
      if @facture.update_attributes(params[:facture])
        format.html { redirect_to @facture, :notice => 'Facture was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @facture.errors, :status => :unprocessable_entity }
      end
    end
  end        

2 个答案:

答案 0 :(得分:1)

你应该用这个:

select_tag "facture[drinks]", options_for_select([ "Water", "Soda", "Beer" ], "Water")

create方法使用params [:facture]中发送的值,使用“facture [drinks]”将其包含为params [:facture] [:drinks]。

答案 1 :(得分:0)

以下是我的表现(感谢miligraf):

1-让控制器中的代码完整无缺

2-更改views / factures / _form.html.erb中的代码:

<强>前

<%= select_tag "facture", options_for_select([ "Water", "Soda", "Beer" ], "Water") %>

<强>后

<%= select_tag "facture[drinks]", options_for_select([ "Water", "Soda", "Beer" ], "Water") %>