如何使用与form_for的关系编辑模型?

时间:2014-04-14 08:37:18

标签: ruby-on-rails ruby form-for

我在rails中使用form_for - 标记时遇到问题。

我有以下型号:

假日

   id      |   name
___________|_____________
   123     |   "asdf"
   345     |   "foo"
   757     |   "bar"

Holidates

   id      |   date
___________|_____________
   223     |   1998-03-11
   445     |   1999-06-21
   557     |   2009-09-20

国家

   id      |   name      |   country_code
___________|_____________|________________
   667     |   "italia"  |   "it"
   987     |   "france"  |   "fr"
   999     |   "austria" |   "au"

R_Countries

   id      |   country_id|   holiday_id
___________|_____________|________________
   463     |   667       |   123
   574     |   987       |   345
   687     |   999       |   757

R_Holidates

   id      |   holidate_id|   holiday_id
___________|______________|________________
   142     |   123        |   223
   253     |   345        |   445
   362     |   757        |   557

我想用一个表单创建一个假期,名字,日期和国家,所以这将是我的_form:

<%= form_for(@holiday) do |f| %>
  <% if @holiday.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@holiday.errors.count, "error") %> prohibited this timespan from being saved:</h2>
      <ul>
      <% @holiday.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :holidate %><br>
    <%= f.date_select :holidate %>
  </div>
  <div class="field">
    <%= f.label :country_code %><br>
    <%= f.date_select :country_code %>
  </div>
  <div class="field">
    <%= f.label :country_name %><br>
    <%= f.text_field :country_name %>
  </div>

  <div class="actions">
   <%= f.submit %>
  </div>
<% end %>

但是,当然,在执行脚本时,我收到此错误:

假期#编辑中的

NoMethodError 显示C:/xampp/htdocs/fluxcapacitor/app/views/holidays/_form.html.erb,其中第20行引发: 用于#

的未定义方法`holidate'

编辑: 这是一个设计示例,它应该如何显示: Layout


EDIT2: 我用我的种子文件填充了我的数据库,在那里我使用了一个脚本:

from = Date.civil(2000,1,1)                                                         
to = Date.civil(2000,12,31)                                                             
Holidays.between(from, to, :any ).each do |date|                                    
    lname = date[:name]
    ldate = date[:date]
    lregion = date[:regions]
    Holiday.find_or_create_by(name:  lname)             #Holiday is created
    Holidate.find_or_create_by(date: ldate)                                                                                  #creates a Holidate
    RHolidate.find_or_create_by(holidate_id: Holidate.find_by(date: ldate).id, holiday_id: Holiday.find_by(name: lname).id)  #this should create a relation between the holiday and it's date
    ####
    lregion.each do |x|
        Country.create(country_code: x, name: x)
        if  !(RCountry.exists?(country_id: Country.find_by(country_code: x).id, holiday_id: Holiday.find_by(name: lname).id))
            RCountry.create(country_id: Country.find_by(country_code: x).id, holiday_id: Holiday.find_by(name: lname).id)
        end
    end

end 

1 个答案:

答案 0 :(得分:1)

我不确定你的代码,但你的表单看起来像(根据你的需要改进):

<%= form_for(@holiday) do |f| %>
  ........some code about errors here....


 <%= fields_for :holydate, @holyday.holydate do |holydate| %>
   <%= holydate.date_select :holidate  %>
  <% end %>

  ........some other code here....

   <%= f.submit %>
<% end %>

或者有另一种方法可以使用标签并在创建/更新方法中调用它们:

  <div class="field">
    <%= label_tag 'Date' %><br />
    <%= date_field_tag 'holidate' %>
  </div>
  <div class="field">
    <%= label_tag 'Countrycode' %><br />
    <%= text_field_tag :country_code %>
  </div>
  <div class="field">
    <%= label_tag 'Countryname' %><br />
    <%= text_field_tag 'country_name' %>
  </div>

这也是控制器方法:

  def create
    @holiday = Holiday.new(holiday_params)
    e=Date.civil(d[:year].to_i,d[:month].to_i,d[:day].to_i)
    holidate=Holidate.where(:date => e).first_or_create
    RHolidate.where(:holidate_id => holidate.id, :holiday_id => @holiday.id ).first_or_create
    country=Country.where(:name => params[country_name], :country_code => params[country_code] ).first_or_create
    RCountry.where(:country_id => country.id, :holiday_id => @holiday.id ).first_or_create
    respond_to do |format|
      if @holiday.save
        format.html { redirect_to @holiday, notice: 'Holiday was successfully created.' }
        format.json { render action: 'show', status: :created, location: @holiday }
      else
        format.html { render action: 'new' }
        format.json { render json: @holiday.errors, status: :unprocessable_entity }
      end
    end
  end