Ruby on Railsで紹介するアプリを作っています。acthive_storageを使用して、画像を表示させたいのですが、showページでは表示されますが、indexページではNoMethodError が出ます。
view/index.html.erbの記入方法かと思うのですが、上手くいかないので解決策を教えて欲しいです。
view/index.html.erb
<h3>犬情報一覧</h3> <table> <thead> <tr> <th>写真</th> <th>id</th> <th>名前</th> <th>犬種</th> <th>年齢</th> <th>一言紹介</th> <th>性格</th> <th>住所</th> </tr> </thead> <tbody> <% @dogs.each do |dog| %> <tr> <td> <%= image_tag @dogs.image ,class:'dog-image'%></td> <td>No.<%= dog.id %></td> <td><%= dog.name %>ちゃん</td> <td><%= dog.kind %></td> <td><%= dog.old %>歳</td> <td><%= dog.dog_pr %></td> <td><%= dog.personality %></td> <td><%= dog.address %></td> </tr> <% end %> </tbody> </table> <p><%= link_to "ワンちゃん新規登録", :new_dog %></p> ```show.html.erb ```ここに言語を入力 <p id="notice"><%= notice %></p> <div class="dog-show-wrap"> <div class="dog-show-image"> <%= image_tag @dog.image ,class:'dog-show-image'%> </div> <div class="dog-show-text"> <p class="showText"><%= @dog.name %>ちゃん</p> <p class="showText"><%= @dog.old %></p> <p class="showText"><%= @dog.kind %></p> <p class="showText"><%= @dog.dog_pr %></p> <p class="showText"><%= @dog.personality %></p> <p class="showText"><%= @dog.address %></p> </div> </div> <%= link_to 'Edit', edit_dog_path(@dog) %> | <%= link_to 'Back', dogs_path %>
dogs_controllers
class DogsController < ApplicationController before_action :set_dog, only: %i[ show edit update destroy ] # GET /dogs or /dogs.json def index @dogs = Dog.all end # GET /dogs/1 or /dogs/1.json def show end # GET /dogs/new def new @dog = Dog.new end # GET /dogs/1/edit def edit end # POST /dogs or /dogs.json def create @dog = Dog.new(dog_params) respond_to do |format| if @dog.save format.html { redirect_to dog_url(@dog), notice: "Dog was successfully created." } format.json { render :show, status: :created, location: @dog } else format.html { render :new, status: :unprocessable_entity } format.json { render json: @dog.errors, status: :unprocessable_entity } end end end # PATCH/PUT /dogs/1 or /dogs/1.json def update respond_to do |format| if @dog.update(dog_params) format.html { redirect_to dog_url(@dog), notice: "Dog was successfully updated." } format.json { render :show, status: :ok, location: @dog } else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @dog.errors, status: :unprocessable_entity } end end end # DELETE /dogs/1 or /dogs/1.json def destroy @dog.destroy respond_to do |format| format.html { redirect_to dogs_url, notice: "Dog was successfully destroyed." } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_dog @dog = Dog.find(params[:id]) end # Only allow a list of trusted parameters through. def dog_params params.require(:dog).permit(:name,:kind,:address,:old,:dog_pr,:personality,:image) end end
models/dogs.rb
class Dog < ApplicationRecord has_one_attached :image end
routes.rb
routes.rb
1Rails.application.routes.draw do 2 resources :dogs 3 end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/29 01:59