work
ペーパークリップから JSON への画像 URL をスキャフォールドに含めることができないようです。dom に対して ajax 操作を行うには、この URL が必要です。
私の理解では、モデルに定義を追加してから、コントローラーの format.json に余分な行を追加します。
@work.avatar_url をコントローラー メソッドに追加するとdef create
、構文エラーが発生します。
syntax error, unexpected ',', expecting tASSOC
私はレールとMVCに本当に慣れていません。すべての答えはとても簡単に聞こえます。残念ながら、私は推測して確認しているだけです...
コントローラーリンク: works_controller.rb
マイデフクリエイト
def create
@work = Work.new(params[:work])
respond_to do |format|
if @work.save
format.html { redirect_to @work, notice: 'Work was successfully created.' }
format.json { render json: @work, status: :created, location: @work }
else
format.html { render action: "new" }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
end
end
Git リンク モデル: work.rb
class Work < ActiveRecord::Base
validates :name, :presence => true
has_many :categoryworks
has_many :categories, :through => :categoryworks
accepts_nested_attributes_for :categories
attr_accessible :name, :subtitle, :category_ids, :svg, :post_a, :post_b, :post_c, :post_d, :avatar
has_attached_file :avatar, :styles => { :medium => "1280x700>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
def avatar_url
avatar.url(:medium)
end
end