我正试图通过下拉菜单显示用户的家乡。城市名称列表在我的seeds.rb文件中并且运行良好。但是,我想在城市名称(纽约自由女神像,圣弗朗金门大桥等照片)旁边拍照。我正在使用回形针,从未在我的种子文件上做过这样的事情之前。
错误 使用回形针添加头像并重新启动服务器时出现此错误 - >我相信它是由于我的观点和种子.rb - > #l /
的未定义方法`avatar'User.rb
class User < ApplicationRecord
belongs_to :homecity, optional: true
belongs_to :university, optional: true
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
end
Homecity.rb
class Homecity < ApplicationRecord
has_many :users
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/liberty.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
end
homecities_controller.rb
class HomecitiesController < ApplicationController
before_action :authenticate_user!, only: [:new, :create]
def create
@homecity = Homecity.new(homecity_params)
if @homecity.save
render json: @homecity
else
render json: {errors: @homecity.errors.full_messages}
end
end
private
def homecity_params
params.require(:homecity).permit(:Hometown, :avatar)
end
end
Seeds.rb
Homecity.destroy_all
bigapple = Homecity.create!(Hometown:"New York City", avatar)
techhub = Homecity.create!(Hometown:"San Francisco", avatar)
longhorns = Homecity.create!(Hometown:"Austin", avatar)
angels = Homecity.create!(Hometown:"Los Angeles", avatar)
Edit.html.erb
<div class="field">
<%= f.label :homecity, "Home Town" %><br>
<%= f.select :homecity_id, Homecity.all.pluck(:Hometown, :id), Homecity.avatar, {}, { class: "selectize1" } %>
</div>
edit.js
$(document).on("turbolinks:load", function() {
var selectizeCallback = null;
$(".homecity-modal").on("hide.bs.modal", function(e) {
if (selectizeCallback != null) {
selectizeCallback();
selecitzeCallback = null;
}
$("#new_homecity").trigger("reset");
$.rails.enableFormElements($("#new_homecity"));
});
$("#new_homecity").on("submit", function(e) {
e.preventDefault();
$.ajax({
method: "POST",
url: $(this).attr("action"),
data: $(this).serialize(),
success: function(response) {
selectizeCallback({value: response.id, text: response.Hometown});
selectizeCallback = null;
$(".homecity-modal").modal('toggle');
}
});
});
$(".selectize1").selectize({
maxOptions: 3,
create: function(input, callback) {
selectizeCallback = callback;
$(".homecity-modal").modal();
$("#homecity_Hometown").val(input);
}
});
});