Я пытаюсь отобразить одно изображение на моей странице указателя для тех заданий, которые были созданы в моем проекте.Я использую Rails 5.2 и Active Storage для обработки изображений.Я следовал руководству, чтобы добраться до этой точки, но теперь не уверен, почему изображения не отображаются.Мой код ниже:
**** Job Controller ****
class JobsController < ApplicationController
before_action :set_job , except: [ :index, :new, :create, :edit, :delete_image_attachment ]
before_action :authenticate_user!, except: [:show]
before_action :is_authorised, only: [:active, :budget, :description, :photo_upload, :address,
:update, :show, :delete, :featured, :premium ]
def active
end
# GET /jobs
def index
@jobs = current_user.jobs
end
# GET /jobs/1
def show
end
# GET /jobs/new
def new
@job = current_user.jobs.build
end
def listing
#@job = current_user.job
#@job = Job.find(params[:id])
end
# GET /jobs/1/edit
def edit
end
def budget
end
def address
end
# POST /jobs
def create
@job = current_user.jobs.build(job_params)
if @job.save!
redirect_to listing_job_path(@job), notice: 'Jobs dev was successfully created.'
else
render :new
end
end
# PATCH/PUT /jobs/1
# def update
# if @job.update(job_params)
# redirect_to @job, notice: 'Jobs dev was successfully updated.'
# else
# render :edit
# end
# end
def update
new_params = job_params
new_params = job_params.merge(active: true) if is_ready_job
#respond_to do |format|
if @job.update(new_params)
#format.html { redirect_to @job, notice: 'Post was successfully updated.' }
#format.json { render :show, status: :ok, location: @job }
flash[:notice] = 'Saved...'
else
#format.html { render :edit }
#format.json { render json: @job.errors, status: :unprocessable_entity }
flash[:alert] = "Something went wrong..."
end
redirect_back(fallback_location: request.referer)
#end
end
# DELETE /jobs/1
def destroy
@job.destroy
redirect_to jobs_url, notice: 'Job was successfully destroyed.'
end
def delete
end
def featured
#@job = current_user.job
end
def premium
#@job = current_user.job
end
# DELETE job images
def delete_image_attachment
@image = ActiveStorage::Attachment.find(params[:image_id])
@image.purge
redirect_back(fallback_location: request.referer)
respond_to :js
end
private
# Use callbacks to share common setup or constraints between actions.
def set_job
@job = Job.find(params[:id])
end
def is_ready_job
!@job.active && !@job.budget.blank? && !@job.job_title.blank? && @job.images.attached? && !@job.address.blank?
end
def is_authorised
redirect_to root_path, alert: "You don't have permission..." unless current_user.id == @job.user_id
end
# Only allow a trusted parameter "white list" through.
def job_params
params.require(:job).permit(:index, :active, :job_category, :job_type, :job_title, :job_description, :recurrence, :address, :listing,
:featured, :premium, :budget, images: []
)
end
end
**** Job Model ****
class Job < ApplicationRecord
belongs_to :user
#belongs_to :job_category
#belongs_to :job_type
has_many_attached :images
validates :job_category, presence: true
validates :job_type, presence: true
validates :recurrence, presence: true
#validates :job_title, presence: true
#validates :job_description, presence: true
end
**Мой взгляд **
<% if @job.images.attached? %>
<div class="row">
<% (0...@job.images.count).each do |image| %>
<div class="col-md-3">
<ul class="sidebar-list">
<li class="sidebar-item"><%= link_to "Listed Jobs", jobs_path, class: "sidebar-link active" %></li>
<li class="sidebar-item"><%= link_to "Assigned Jobs", jobs_path, class: "sidebar-link active" %></li>
</ul>
</div>
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">
Jobs
</div>
<div class="panel-body">
<% @jobs.each do |job| %>
<div class="row">
<div class="col-md-2">
<%= image_tag(@job.images[image]) %>
</div>
<div class="col-md-7">
<h4><%= job.job_title %></h4>
</div>
<div class="col-md-3 right">
<%= link_to "Update", listing_job_path(job), class: "btn btn-form" %>
</div>
</div><hr/>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
<% end %>
***** Ошибка *****
ActionView::Template::Error (undefined method `images' for nil:NilClass):
1: <% if @job.images.attached? %>
2: <div class="row">
3: <% (0...@job.images.count).each do |image| %>
4: <div class="col-md-3">
app/views/jobs/index.html.erb:1:in `_app_views_jobs_index_html_erb__713310483_39585312'
Я не уверен, почему я получаю этот неопределенный метод 'images', потому что у меня естьвызывал и использовал @ job.images [image] в другом месте моего кода, который, кажется, работает нормально.Идеи ??