下拉菜单栏

时间:2015-09-15 19:29:38

标签: ruby-on-rails ruby-on-rails-4

我目前在“'类别”中的数据库中播种了类别。模型,我试图做的是在新帖子页面上有一个下拉列表,它允许我选择它属于哪个类别。

我现在遇到的问题是:

enter image description here

这是我目前在表单中使用的字段。

  <div class="field">
    <%= f.select :category, Category.all, :prompt => "Select One" %>
  </div>

任何帮助都会很棒。

由于

更新

提供控制器创建

  def create
    @offer = Offer.new(offer_params)

    respond_to do |format|
      if @offer.save
        format.html { redirect_to @offer, notice: 'Offer was successfully created.' }
        format.json { render :show, status: :created, location: @offer }
      else
        format.html { render :new }
        format.json { render json: @offer.errors, status: :unprocessable_entity }
      end
    end
  end

2 个答案:

答案 0 :(得分:2)

尝试使用:

Private Sub CommandButton1_Click()
ActiveWorkbook.Charts.Add
Dim ws As Worksheet
Set ws = tracker
Dim i As Long
For i = 2 To ws.Range("A65536").End(xlUp).Row
Charts.Add
With ActiveWorkbook.ActiveChart
    'Data?
    .ChartType = xlXYScatter
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = "Progress"
    .SeriesCollection(1).XValues = "=tracker!$B$1:$J$1"
    .SeriesCollection(1).Values = "=tracker!$B$" & i & ":$J$" & i

    'Titles
    .HasTitle = True
    .ChartTitle.Characters.Text = Range("N" & i).Value
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Timeline"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Grade"
    .Axes(xlCategory).HasMajorGridlines = True

    'Formatting
    .Axes(xlCategory).HasMinorGridlines = False
    .Axes(xlValue).HasMajorGridlines = True
    .Axes(xlValue).HasMinorGridlines = False
    .HasLegend = False

End With
Next
End Sub

可以找到更多信息here

不要忘记将<%= f.select :category_id, Category.all.collect{|c| [c.name, c.id]}, :prompt => "Select One" %> 添加到控制器中:category_id的允许参数列表中。

答案 1 :(得分:-1)

而不是执行Category.all - 它返回一个ActiveRecord对象,你必须在你想要的对象上指定属性。

有一个内置的表单助手:

<%= f.collection_select(:category_id, Category.all, :id, :name) %>

表单助手的Rails指南可能有所帮助:http://guides.rubyonrails.org/form_helpers.html