【Rails5】エラー:undefined method `url' for nil:NilClassの解決方法を教えてください。
解決したいこと
Rails5でレシピサイトを作っています。
料理の作り方の画像を表示したいのですが、上記のエラーが発生しました。
解決方法を教えてください。
該当するソースコード
show.html.erb
<div class="card">
<% @how_to_makes.each do |process| %>
<ol>
<li>
<%= image_tag process.process_image.url, class: "card-img-top" %>
<div class="card-body"><%= process.explanation %></div>
</li>
</ol>
<% end %>
</div>
上記の<%= image_tag process.process_image.url, class: "card-img-top" %>
の行でエラーが発生しています。
他関連コード
recipe.rb
class Recipe < ApplicationRecord
belongs_to :user, optional: true
has_many :recipe_ingredients, dependent: :destroy
has_many :how_to_makes, dependent: :destroy
accepts_nested_attributes_for :recipe_ingredients, allow_destroy: true
accepts_nested_attributes_for :how_to_makes, allow_destroy: true
mount_uploader :image, ImageUploader
mount_uploader :process_image, ProcessImageUploader
end
migrate/20210116144054_create_how_to_makes.rb
class CreateHowToMakes < ActiveRecord::Migration[5.2]
def change
create_table :how_to_makes do |t|
t.references :recipe, foreign_key: true
t.text :explanation
t.string :process_image
t.integer :order_no
t.timestamps
end
end
end
recipes_controller.rb
class RecipesController < ApplicationController
before_action :require_user_logged_in, only: [:new, :create, :edit, :update, :destroy]
def show
@recipe = Recipe.find(params[:id])
@recipe_ingredients = @recipe.recipe_ingredients.all
@how_to_makes = @recipe.how_to_makes.all
end
def new
@recipe = Recipe.new
@recipe_ingredients = @recipe.recipe_ingredients.build
@how_to_makes = @recipe.how_to_makes.build
end
def create
@recipe = Recipe.new(recipe_params)
if @recipe.save
flash[:success] = 'レシピを投稿しました。'
redirect_to root_url
else
flash.now[:danger] = 'レシピの投稿に失敗しました。'
render :new
end
end
private
def recipe_params
params.require(:recipe).permit(:title, :catchcopy, :no_of_dish, :image,
recipe_ingredients_attributes:[:id, :recipe_id, :ing_name, :quantity, :_destroy],
how_to_makes_attributes:[:id, :explanation, :process_image, :order_no, :_destroy])
end
end
new.html.erb
<div class="recipe-post">
<%= form_with(model: @recipe, local: true) do |f| %>
<div class="form-group mt-2">
<%= f.fields_for :how_to_makes do |k| %>
<%= render "recipes/how_to_make_fields", f: k %>
<% end %>
</div>
<div id="detail-association-insertion-point2"></div>
<div class="col-10 mx-auto">
<%= link_to_add_association "+フォームを追加", f, :how_to_makes,
class: "btn btn-secondary btn-block",
data: {
association_insertion_node: '#detail-association-insertion-point2',
association_insertion_method: 'after'
}%>
</div>
<div class="col-10 mx-auto">
<%= f.submit 'レシピ投稿!', class: 'mt-2 btn btn-secondary btn-block' %>
</div>
<% end %>
</div>
recipes/_how_to_make_fields.html
<div class="nested-fields">
<div class="row">
<div class="col-5"><%= f.text_area :explanation, class: "form-control", placeholder: "説明" %></div>
<div><%= f.file_field :process_image %></div>
<div class="col-1 px-0 w-auto">
<%= link_to_remove_association "削除", f, class: "btn btn-secondary btn-block" %>
</div>
</div>
</div>
お手数おかけしますが、宜しくお願い致します。
0