レシピをアップロードできるシンプルなアプリがあります
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")
誰かがそれを助けることができれば、それは大歓迎です
ありがとう