Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在上使用嵌套表单自动完成有多个:通过关系_Ruby On Rails_Ruby_Ruby On Rails 3_Nested Forms_Getter Setter - Fatal编程技术网

Ruby on rails 在上使用嵌套表单自动完成有多个:通过关系

Ruby on rails 在上使用嵌套表单自动完成有多个:通过关系,ruby-on-rails,ruby,ruby-on-rails-3,nested-forms,getter-setter,Ruby On Rails,Ruby,Ruby On Rails 3,Nested Forms,Getter Setter,我目前正在使用嵌套表单gem(Ryan Bates)使用嵌套表单向食谱添加配料。一切都很好。我的下一个目标是在配料中添加自动完成功能,但我一直在考虑如何设置它 一个配方可以有很多成分,一种成分可以有很多配方。我使用了一个has\u many:through关系来管理逻辑 recipe.rb has_many :recipe_ingredients has_many :ingredients, through: :recipe_ingredients accepts_nested_a

我目前正在使用嵌套表单gem(Ryan Bates)使用嵌套表单向食谱添加配料。一切都很好。我的下一个目标是在配料中添加自动完成功能,但我一直在考虑如何设置它

一个配方可以有很多成分,一种成分可以有很多配方。我使用了一个has\u many:through关系来管理逻辑

recipe.rb

  has_many :recipe_ingredients
  has_many :ingredients, through: :recipe_ingredients

  accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:name].blank? }, allow_destroy: true

  attr_accessible :name, :desc, :ingredients_attributes

  validates_presence_of :name
  has_many :recipe_ingredients
  has_many :recipes, through: :recipe_ingredients

  attr_accessible :name, :protein, :carbs, :fat, :ingredient_name

  validates_presence_of :name, :protein, :carbs, :fat

  def ingredient_name
    try(:name)
  end

  def ingredient_name=(name)
    Ingredient.find_by_name(name) if name.present?
  end
  belongs_to :recipe
  belongs_to :ingredient

  attr_accessible :ingredient_id

  validates_presence_of :ingredient_id, :recipe_id
<%= nested_form_for(@recipe) do |f| %>
  <% if @recipe.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>

      <ul>
      <% @recipe.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, placeholder: 'eg. Fajita Magic Sunrise' %>
  </div>
  <div class="field">
    <%= f.label :desc, 'Description' %><br />
    <%= f.text_area :desc, rows: 5, placeholder: 'Steps are useful here...' %>
  </div>

  <h3>Ingredients</h3>
  <%= f.fields_for :ingredients do |builder| %>
      <%= render "ingredient_fields", :f => builder %>
  <% end %>
  <p><%= f.link_to_add "Add Ingredient", :ingredients %></p>

  <div class="actions">
    <%= f.submit nil, class: 'button green' %> or <%= link_to 'go back', :back %>
  </div>
<% end %>
<div class="field">
  <%= f.label :name, "Name" %><br />
  <%= f.text_field :ingredient_name, placeholder: 'Eg. 1 Scrambled Egg' %> 
  <%= f.text_field :protein, placeholder: "Protein", size: 5 %>
  <%= f.text_field :carbs, placeholder: "Carbs", size: 5 %>
  <%= f.text_field :fat, placeholder: "Fat", size: 5 %>
  <%= f.link_to_remove "Remove" %>
</div>
配料.rb

  has_many :recipe_ingredients
  has_many :ingredients, through: :recipe_ingredients

  accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:name].blank? }, allow_destroy: true

  attr_accessible :name, :desc, :ingredients_attributes

  validates_presence_of :name
  has_many :recipe_ingredients
  has_many :recipes, through: :recipe_ingredients

  attr_accessible :name, :protein, :carbs, :fat, :ingredient_name

  validates_presence_of :name, :protein, :carbs, :fat

  def ingredient_name
    try(:name)
  end

  def ingredient_name=(name)
    Ingredient.find_by_name(name) if name.present?
  end
  belongs_to :recipe
  belongs_to :ingredient

  attr_accessible :ingredient_id

  validates_presence_of :ingredient_id, :recipe_id
<%= nested_form_for(@recipe) do |f| %>
  <% if @recipe.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>

      <ul>
      <% @recipe.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, placeholder: 'eg. Fajita Magic Sunrise' %>
  </div>
  <div class="field">
    <%= f.label :desc, 'Description' %><br />
    <%= f.text_area :desc, rows: 5, placeholder: 'Steps are useful here...' %>
  </div>

  <h3>Ingredients</h3>
  <%= f.fields_for :ingredients do |builder| %>
      <%= render "ingredient_fields", :f => builder %>
  <% end %>
  <p><%= f.link_to_add "Add Ingredient", :ingredients %></p>

  <div class="actions">
    <%= f.submit nil, class: 'button green' %> or <%= link_to 'go back', :back %>
  </div>
<% end %>
<div class="field">
  <%= f.label :name, "Name" %><br />
  <%= f.text_field :ingredient_name, placeholder: 'Eg. 1 Scrambled Egg' %> 
  <%= f.text_field :protein, placeholder: "Protein", size: 5 %>
  <%= f.text_field :carbs, placeholder: "Carbs", size: 5 %>
  <%= f.text_field :fat, placeholder: "Fat", size: 5 %>
  <%= f.link_to_remove "Remove" %>
</div>
配方\u配料.rb

  has_many :recipe_ingredients
  has_many :ingredients, through: :recipe_ingredients

  accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:name].blank? }, allow_destroy: true

  attr_accessible :name, :desc, :ingredients_attributes

  validates_presence_of :name
  has_many :recipe_ingredients
  has_many :recipes, through: :recipe_ingredients

  attr_accessible :name, :protein, :carbs, :fat, :ingredient_name

  validates_presence_of :name, :protein, :carbs, :fat

  def ingredient_name
    try(:name)
  end

  def ingredient_name=(name)
    Ingredient.find_by_name(name) if name.present?
  end
  belongs_to :recipe
  belongs_to :ingredient

  attr_accessible :ingredient_id

  validates_presence_of :ingredient_id, :recipe_id
<%= nested_form_for(@recipe) do |f| %>
  <% if @recipe.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>

      <ul>
      <% @recipe.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, placeholder: 'eg. Fajita Magic Sunrise' %>
  </div>
  <div class="field">
    <%= f.label :desc, 'Description' %><br />
    <%= f.text_area :desc, rows: 5, placeholder: 'Steps are useful here...' %>
  </div>

  <h3>Ingredients</h3>
  <%= f.fields_for :ingredients do |builder| %>
      <%= render "ingredient_fields", :f => builder %>
  <% end %>
  <p><%= f.link_to_add "Add Ingredient", :ingredients %></p>

  <div class="actions">
    <%= f.submit nil, class: 'button green' %> or <%= link_to 'go back', :back %>
  </div>
<% end %>
<div class="field">
  <%= f.label :name, "Name" %><br />
  <%= f.text_field :ingredient_name, placeholder: 'Eg. 1 Scrambled Egg' %> 
  <%= f.text_field :protein, placeholder: "Protein", size: 5 %>
  <%= f.text_field :carbs, placeholder: "Carbs", size: 5 %>
  <%= f.text_field :fat, placeholder: "Fat", size: 5 %>
  <%= f.link_to_remove "Remove" %>
</div>
recipes/_form.html.erb

  has_many :recipe_ingredients
  has_many :ingredients, through: :recipe_ingredients

  accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:name].blank? }, allow_destroy: true

  attr_accessible :name, :desc, :ingredients_attributes

  validates_presence_of :name
  has_many :recipe_ingredients
  has_many :recipes, through: :recipe_ingredients

  attr_accessible :name, :protein, :carbs, :fat, :ingredient_name

  validates_presence_of :name, :protein, :carbs, :fat

  def ingredient_name
    try(:name)
  end

  def ingredient_name=(name)
    Ingredient.find_by_name(name) if name.present?
  end
  belongs_to :recipe
  belongs_to :ingredient

  attr_accessible :ingredient_id

  validates_presence_of :ingredient_id, :recipe_id
<%= nested_form_for(@recipe) do |f| %>
  <% if @recipe.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>

      <ul>
      <% @recipe.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, placeholder: 'eg. Fajita Magic Sunrise' %>
  </div>
  <div class="field">
    <%= f.label :desc, 'Description' %><br />
    <%= f.text_area :desc, rows: 5, placeholder: 'Steps are useful here...' %>
  </div>

  <h3>Ingredients</h3>
  <%= f.fields_for :ingredients do |builder| %>
      <%= render "ingredient_fields", :f => builder %>
  <% end %>
  <p><%= f.link_to_add "Add Ingredient", :ingredients %></p>

  <div class="actions">
    <%= f.submit nil, class: 'button green' %> or <%= link_to 'go back', :back %>
  </div>
<% end %>
<div class="field">
  <%= f.label :name, "Name" %><br />
  <%= f.text_field :ingredient_name, placeholder: 'Eg. 1 Scrambled Egg' %> 
  <%= f.text_field :protein, placeholder: "Protein", size: 5 %>
  <%= f.text_field :carbs, placeholder: "Carbs", size: 5 %>
  <%= f.text_field :fat, placeholder: "Fat", size: 5 %>
  <%= f.link_to_remove "Remove" %>
</div>
所以!正如您所看到的,我设置了一个名为component_name的新属性,并在components.rb中创建了一个getter/setter方法来提取它并设置它(如果它存在)。基本上,我认为问题在于我使用的has_many:through

我现在所拥有的似乎不会更新联接表,因为配方没有传递给我的setter方法,所以它无论如何也不知道要保存到哪个配方


那么虚拟属性是否需要在配方模型中?正如您所看到的,只是非常困惑。

我认为setter方法在上面的实现中甚至没有启动。我向它添加了一个日志,只是为了检查正在发生的事情,甚至删除了它,并且没有抛出错误。我开始认为我对setter使用了错误的方法名。添加了development.log代码段。你找到答案了吗?你找到答案了还是解决了问题?