[英]Paperclip not saving image in model
I am using Paperclip to add images to my model AgentActivity, it is showing on the form as it should however when I update my form it does not seem to save. 我正在使用Paperclip将图像添加到我的模型AgentActivity中,它应该显示在表单上,但是当我更新表单时,它似乎没有保存。 My controller update method is saving all params except image so I think it is setup correctly, however I cannot see where this is going wrong? 我的控制器更新方法正在保存除图像以外的所有参数,因此我认为它设置正确,但是我看不到哪里出错了? I have also set up the model.rb file as per documentation provided... 我还根据提供的文档设置了model.rb文件...
Form 形成
<%= image_tag @submission.agent_activities.last.image.url(:large) %>
<%= form_for @submission, html: { multipart: true } do |f| %>
<%= f.fields_for :agent_activity do |a|%>
<td> <div class="field">
<%= select_tag(:Status, options_for_select([['In Progress', 1], ['Not Interested', 2],['Viewing Arranged', 3]])) %>
</div>
</td>
<td> <div class="field">
<%= text_field_tag :Notes %>
</div>
</td>
<%= f.file_field :image %>
<td>
<div class="actions">
<%= f.submit %>
</div>
</td>
<% end %>
<% end %>
Controller update method 控制器更新方法
def update
respond_to do |format|
if user_signed_in?
@submission.update(submission_params)
format.html { redirect_to @submission, notice: 'Submission was successfully updated.' }
format.json { render :show, status: :ok, location: @submission }
else
if agent_signed_in?
#AgentActivity.create(agent_activity_params)
AgentActivity.create(agent_id: current_agent.id, submission_id: @submission.id, Notes: agent_activity_params[:Notes], Status: agent_activity_params[:Status] , image: agent_activity_params[:image])
format.html { redirect_to @submission, notice: 'Activity was successfully updated.' }
format.json { render :show, status: :ok, location: @submission }
else
format.html { render :edit }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
Params PARAMS
def agent_activity_params
params.permit(:id, @submission.id, :Status, :Notes, :image)
end
agent_activity.rb agent_activity.rb
class AgentActivity < ApplicationRecord
belongs_to :submission, :optional => true, inverse_of:
:agent_activities #has submission_id foreign key in table
belongs_to :agent, :optional => true, inverse_of: :agent_activities
has_attached_file :image, styles: { large: "600x600>", medium: "300x300>", thumb: "100x100#" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end 结束
Schema.rb Schema.rb
create_table "agent_activities", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "Status"
t.text "Notes"
t.bigint "agent_id"
t.bigint "submission_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.index ["agent_id"], name: "index_agent_activities_on_agent_id"
t.index ["submission_id"], name: "index_agent_activities_on_submission_id"
end
Avoid paperclip. 避免回形针。 Use rails own feature ActiveStorage because paperclip is deprecated. 使用Rails自己的功能ActiveStorage,因为回形针已弃用。 You can get details in paperclip gem documentation. 您可以在回形针gem文档中获取详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.