关联时键入不匹配,f。选择rails

时间:2017-09-14 10:41:30

标签: ruby-on-rails

我的表格有问题,我想在我的“会员”表格中创建一个条目,每个成员都连接到“年份”,但我一直选择不匹配的年份。

这是我的表格:

<div class="field">
<%= f.label :name %><br>
<%= f.text_area :name %>
</div>
<div class="field">
<%= f.label :nickname %><br>
<%= f.text_area :nickname %>
</div>
<div class="field">
<%= f.label :year %>
<%= f.select :year, options_for_select(@years.all.map{|y| [y.year,y.id]}) %>
</div>
<div class="actions">
<%= f.submit %>
</div>

以下是模特:

class Member < ApplicationRecord
   belongs_to :year
   mount_uploader :image, ImageUploader
end

class Year < ApplicationRecord
    has_many :members, dependent: :destroy
end

当我尝试提交时,我收到以下错误:

年(#70050157849460)预期,获得“1”这是一个字符串的实例(#9412380)

我哪里出错了?

编辑:

以下是控制器的代码

class MembersController < ApplicationController
  def home
    @members = Member.all
  end
  def new
    @member = Member.new
    @years = Year.all
  end
  def create
    @member = Member.new(member_params)
    if @member.save
      flash[:success] = "Member Created"
      redirect_to root_path
    else
      render 'form'
    end
  end

  private

  def member_params
    params.require(:member).permit(:name,:nick,:position,:image,:year)
  end

end

1 个答案:

答案 0 :(得分:0)

data("WeatherPlay", package = "partykit")

#create a split
sp_o <- partysplit(3L, breaks = 75) 

#create a node
n1 <- partynode(id = 1L, split = sp_o, kids = lapply(2L:3L, partynode))

#and make a "tree" out of it
t2 <- party(
  n1,
  data = WeatherPlay,
  fitted = data.frame(
    "(fitted)" = fitted_node(n1, data = WeatherPlay),
    "(response)" = WeatherPlay$play,
    check.names = FALSE
  ),
  terms = terms(play ~ ., data = WeatherPlay),
)

t2 <- as.constparty(t2)
t2
plot(t2)

<div class="field">
<%= f.label :name %><br>
<%= f.text_area :name %>
</div>
<div class="field">
<%= f.label :nickname %><br>
<%= f.text_area :nickname %>
</div>
<div class="field">
<%= f.label :year %>
<%= f.select :year_id, options_for_select(@years.all.map{|y| [y.year,y.id]}) %>
</div>
<div class="actions">
<%= f.submit %>
</div>

这第二个我没有检查但是首先会工作你需要说year_id因为当你在集合上映射时你将id设置为正在传递的参数 但是在表单上选择你基本上告诉表单期待活动记录对象。