如果当前不存在,如何获取railsfields_for建立新的关联?

时间:2018-09-08 16:44:09

标签: ruby-on-rails

我正在引用此堆栈溢出帖子:How to get Rails build and fields_for to create only a new record and not include existing?

就我而言,我有这个:

    class MyInput extends React.Component{
    constructor(props){
    super(props);
    this.state = {
      buttonStyle: '',
      voiceSelected: ''
    }
    this.handleChange = this.handleChange.bind(this);
  }

 handleChange(event) {
    event.persist();
     if (event.target.id === 'voiceSelected') {

      //set the state we're going to use later

      this.setState(() => ({ 'voiceSelected': event.target.value}));

      //Take the first word from the string to make it visable on button

      let totalWords = event.target.value
      var firstWord = totalWords.replace(/ .*/,'');
      console.log(firstWord)
      this.setState(() => ({'buttonStyle': firstWord}));



    } else {
      console.log("Something Went Wrong!")
    }
  }

  render(){
    return  <select 
              id="voiceSelected" 
              value={this.state.buttonStyle} 
              name={this.state.buttonStyle} 
              onChange={(e) => {this.handleChange(e)}}
              >
      <option value="" selected disabled hidden>Choose Voice</option>
      <option value="Ivy">Ivy [English - American]</option>
      <option value="Joanna">Joanna [English - American]</option>
      <option value="Joey">Joey [English - American]</option>
      <option value="Justin">Justin [English - American]</option>
      <option value="Kendra">Kendra [English - American]</option>
      <option value="Kimberly">Kimberly [English - American]</option>
      <option value="Salli">Salli [English - American]</option>
      <option value="Nicole">Nicole [English - Australian]</option>
      <option value="Russell">Russell [English - Australian]</option>
      <option value="Emma">Emma [English - British]</option>
      <option value="Brian">Brian [English - British]</option>
      <option value="Amy">Amy [English - British]</option>
      <option value="Raveena">Raveena [English - Indian]</option>
      <option value="Geraint">Geraint [English - Welsh]</option>
      <option value="Ricardo">Ricardo [Brazilian Portuguese]</option>
      <option value="Vitoria">Vitoria [Brazilian Portuguese]</option>
      <option value="Lotte">Lotte [Dutch]</option>
      <option value="Ruben">Ruben [Dutch]</option>
      <option value="Mathieu">Mathieu [French]</option>
      <option value="Celine">Celine [French]</option>
      <option value="Chantal">Chantal [Canadian French]</option>
      <option value="Marlene">Marlene [German]</option>
      <option value="Dora">Dora [Icelandic]</option>
      <option value="Karl">Karl [Icelandic]</option>
      <option value="Carla">Carla [Italian]</option>
      <option value="Giorgio">Giorgio [Italian]</option>
      <option value="Mizuki">Mizuki [Japanese]</option>
      <option value="Liv">Liv [Norwegian]</option>
      <option value="Maja">Maja [Polish]</option>
      <option value="Jan">Jan [Polish]</option>
      <option value="Ewa">Ewa [Polish]</option>
      <option value="Cristiano">Cristiano [Portuquese]</option>
      <option value="Ines">Ines [Portuquese]</option>
      <option value="Carmen">Carmen [Romanian]</option>
      <option value="Maxim">Maxim [Russian]</option>
      <option value="Tatyana">Tatyana [Russian]</option>
      <option value="Enrique">Enrique [Spanish]</option>
      <option value="Penelope">Penelope [US Spanish]</option>
      <option value="Enrique">Miguel [US Spanish]</option>
      <option value="Conchita">Conchita [Castilian Spanish]</option>
      <option value="Astrid">Astrid [Swedish]</option>
      <option value="Filiz">Filiz [Turkish]</option>
      <option value="Gwyneth">Gwyneth [Welsh]</option>
  </select>
  }
}

ReactDOM.render(<MyInput/>, document.getElementById('input'));

如果<%= form.fields_for :campaign_schedule, @campaign.build_campaign_schedule do |campaign_schedule| %> 还没有@campaign,则这将建立一个新的campaign_schedule并以以下形式显示text_fields,以便用户可以为新的{{1}提供数据}。

但是,如果campaign_schedule已经有一个campaign schedule了怎么办?使用此@campaign表单时出现错误,说明:

  

ActiveRecord :: RecordNotSaved(无法删除现有的关联对象   campaign_schedule。该记录的外键被设置后无法保存   设置为零。):

我假设这是因为campaign_schedule已经存在fields_for,并且它正在尝试构建一个新的。

我应该只喜欢这样的东西吗?

campaign_scheduled

或者有更好的方法吗?我基本上有这些关联:

@campaign

<% if @campaign.campaign_schedule.nil? %>
    <%= form.fields_for :campaign_schedule, @campaign.build_campaign_schedule do |campaign_schedule| %>
<% else %>
    <%= form.fields_for :campaign_schedule, do |campaign_schedule| %>
<% end %>

编辑

看起来我的#app/models/campaign.rb class Campaign < ApplicationRecord has_one :campaign_schedule accepts_nested_attributes_for :campaign_schedule end 条件无论如何都不会起作用,因为达到该条件的#app/models/campaign_schedule.rb class CampaignSchedule < ApplicationRecord belongs_to :campaign end if条件发生冲突,因此将不起作用。我想我需要弄清楚只有在不存在关联的情况下,如何操纵end才能建立关联。

1 个答案:

答案 0 :(得分:1)

如果控制器操作无效,我将建立一个新的竞争时间表:

def edit
  @campaign = Campaign.find(params[:id])
  @campaign.build_campaign_schedule unless @campaign.campaign_schedule
end

那么您应该可以:

<%= form.fields_for :campaign_schedule do |campaign_schedule| %>