0

レシピをアップロードできるシンプルなアプリがあります

gem terrarum を統合して、国のモデル内に世界のすべての国を表示しました。レシピが has_one 国で、国がレシピに属している関係があります。私は ryan bates のネストされたフォームを使用していますが、成分モデルと準備モデルから表示する情報を問題なく取得できました。しかし、国名をテーブルに保存したり、ビューに表示したりすることはできません (ただし、これはモデルに保存されていないことが原因です)

コードは次のとおりです

<%= f.label :country_id, "Country Of Origin" %>
<%= f.collection_select(:country_id, Country.all, :id, :name, :prompt => 'Please select country') %>

意見

<% @recipes.each do |r| %>
<tr>
<td><%= r.dish_name %></td>
<td><%= r.country.name %></td>
<td><%= r.difficulty %></td>
<td><%= r.preperation_time %></td>
<td><%= ingredient_names(r.ingredients) %></td>
<td><%= preperation_steps(r.preperations) %></td>
<td><%= image_tag r.avatar.url(:thumb)%></td>
</tr>

ヘルパー

def preperation_steps(preperations)
if preperations
  preperation_array = preperations.map {|pre| pre.prep_steps}
  preperation_array.join("\n")
end
end

def country_name(country)
if country
  country_array = country.map {|c| c.country_name}
  country_array.join("\n")
end
end
end

これが機能するように準備ヘルパーを含めたので、確かに私のcountry_nameヘルパーはこれを反映していますか? または、このためにヘルパーを配置する必要はありませんか?

レシピコントローラー

def new 

@recipes = current_user.recipes if current_user.recipes #show recipes if the user has any recipes
 @favourites = current_user.favourites

end

レシピモデル

  belongs_to :user
  belongs_to :country
  has_many :ingredients 
  has_many :preperations
  has_many :favourites

  attr_accessible :dish_name, :difficulty, :preperation_time, :ingredients_attributes, :preperations_attributes, :country_id, :avatar:preperations_attributes, :country_id, :avatar

  has_attached_file :avatar, :styles => {  :medium => "300x300>", :thumb => "100x100>" } 

  accepts_nested_attributes_for :ingredients, :preperations

  scope :top_countries, order("country_of_origin DESC")

誰かがそれを助けることができれば、それは大歓迎です

ありがとう

4

1 に答える 1

1

このコードには、次の 2 つの間違いがあります。

  1. Recipeする必要がありbelong_to :countryます。ここでは使用しないhas_oneでください。外部キーcountry_idrecipesテーブルにある必要があります。
  2. @recipe.build_countryは必要ありません。すでに国のリストがあります。build_country国のリストに新しい国を追加することを計画している場合にのみ使用してください。この場合はそうではありません。

また、必要ありませんfields_for。あなたはただ行うことができます:

<%= f.label :country_id, "Country Of Origin" %>
<%= f.collection_select(:country_id, Country.all, :id, :name, :prompt => 'Please select country') %>
于 2012-11-11T10:23:38.533 に答える