Я использую камень "Скрепка".Пользователь может отправить местоположение с изображением.Я не хочу, чтобы пользователь дублировал уже отправленное местоположение, но он может добавить к нему изображение.
Я пытался выяснить это.Я оставлю важный код ниже:
location.rb
class Location < ApplicationRecord
has_many :submissions
has_many :users, through: :submissions
# Allows submission objects
accepts_nested_attributes_for :submissions
submission.rb
class Submission < ApplicationRecord
belongs_to :user
belongs_to :location
has_attached_file :image, styles: { large: "600x600>", medium: "300x300>", thumb: "150x150#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
location_controller.rb
class LocationsController < ApplicationController
# Before actions get routed and ran, find_location will occur
before_action :find_location, only: [:show, :edit, :update, :destroy]
# For the Locations/index.html.erb
def index
@locations = Location.all
end
# Binds submission object to the form in new.html.erb
def new
@locations = Location.new
@locations.submissions.build
end
# For creating a new location in the new.html.erb form
def create
@locations = Location.new(user_params)
# Everything went well. User will be sent to @locations show page
if @locations.save
# Redirects to user submitted locations
redirect_to @locations
else
render 'new'
end
end
# Finds new location user submitted by its unique id
def show
end
# Allowing user to update their submitted location
def edit
end
# Updates users edited submission
def update
if @locations.update(user_params)
# Redirects to user submitted locations
redirect_to @locations
else
render 'edit'
end
end
# Deletes users submission
def destroy
@locations.destroy
# Redirects to user submitted locations
redirect_to @locations
end
private
# Used for finding user submitted location (Prevents DRY)
def find_location
@locations = Location.find(params[:id])
end
# Strong parameters for security - Defines what can be update/created in location model
def user_params
params.require(:location).permit(:city, :state, :submissions_attributes => [:image])
end
end
_form.html.erb
<%= form_for @locations, html: {multipart: true} do |f| %>
.
.
.
<!-- User enters image-->
<%= f.fields_for :submissions do |s| %>
<div class="form-group">
<h3>Upload Image:</h3>
<%= s.file_field :image, class: 'form-control' %>
</div>
<% end %>
<% end %>
show.html.erb
<h3>Image: <%= image_tag @locations.image.url(:medium) %></h3>
Я получаю сообщение об ошибке:
"undefined method `image' for.."