我无法上传嵌套脚手架形式的图像。
除图像外,所有其他字段都进入数据库。请帮忙!
我在5.2上,使用回形针进行图像上传,并且需要将图像上传到AWS。
我没有任何错误,只是图像没有进入数据库。
谢谢!
控制器
class ContentsController < ApplicationController
before_action :set_campaign, except: [:your_posts]
before_action :set_content, only: [:show, :edit, :update, :destroy, :approve, :decline]
# GET campaigns/1/contents
def index
@contents = @campaign.contents
end
# GET campaigns/1/contents/1
def show
end
# GET campaigns/1/contents/new
def new
@content = @campaign.contents.build
end
# GET campaigns/1/contents/1/edit
def edit
end
# POST campaigns/1/contents
def create
if !current_user.is_active_influencer
return redirect_to payout_method_path, alert: "Please Connect to Stripe Express first."
end
campaign = Campaign.find(params[:campaign_id])
if campaign.active == false
flash[:alert] = "This Campaign is closed"
elsif current_user == campaign.user
flash[:alert] = "You cannot apply for your own campaign!"
elsif
@content = current_user.contents.build(content_params)
@content.campaign = campaign
@content.transaction_fee = @content.price * 15/100
@content.total = @content.price + @content.transaction_fee
@content.save
flash[:notice] = "Applied Successfully!"
end
redirect_to campaign
end
# PUT campaigns/1/contents/1
def update
if @content.update_attributes(content_params)
redirect_to([@content.campaign, @content], notice: 'Content was successfully updated.')
else
render action: 'edit'
end
end
# DELETE campaigns/1/contents/1
def destroy
@content.destroy
redirect_to campaign_contents_url(@campaign)
end
def your_posts
@posts = current_user.contents
end
def approve
if current_user.stripe_id.blank?
flash[:alert] = "Please update your payment method."
return redirect_to payment_method_path
elsif
charge(@campaign, @content)
elsif
flash[:alert] = "Cannot make a reservation!"
end
end
def decline
@content.Declined!
redirect_to campaign_contents_path
end
private
#Twilio_start
def send_sms(campaign, content)
@client = Twilio::REST::Client.new
@client.messages.create(
from: '+18646060816',
to: content.user.phone_number,
body: "You content for '#{campaign.brand_name}' has been approved."
)
end
#Twilio_end
def charge(campaign, content)
if !campaign.user.stripe_id.blank?
customer = Stripe::Customer.retrieve(campaign.user.stripe_id)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => content.total * 100,
:description => campaign.name,
:currency => "usd",
:destination => {
:amount => content.price * 95, # 95% of the content amount goes to the Content Maker
:account => content.user.merchant_id # Influencer's Stripe customer ID
}
)
if charge
content.Approved!
ContentMailer.send_email_to_influencer(content.user, campaign).deliver_later if content.user.setting.enable_email
send_sms(campaign, content) if content.user.setting.enable_sms #Twilio
flash[:notice] = "Paid and Approved successfully!"
redirect_to campaign_contents_path
else
reservation.Declined!
flash[:alert] = "Cannot charge with this payment method!"
end
end
rescue Stripe::CardError => e
content.Declined!
flash[:alert] = e.message
end
# Use callbacks to share common setup or constraints between actions.
def set_campaign
@campaign = Campaign.find(params[:campaign_id])
end
def set_content
@content = @campaign.contents.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def content_params
params.require(:content).permit(:post_copy, :is_facebook, :is_instagram, :is_twitter, :is_youtube, :price, :images)
end
end
模型
class Content < ApplicationRecord
enum status: {Waiting: 0, Approved: 1, Declined: 2}
after_create_commit :create_notification
belongs_to :user
belongs_to :campaign
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
private
def create_notification
type = self.campaign.Instant? ? "New Booking" : "New Request"
influencer = User.find(self.user_id)
Notification.create(content: "#{type} from #{influencer.fullname}", user_id: self.campaign.user_id)
end
end
观看次数
<div class="panel panel-default">
<div class="panel-heading">
<span><i class="fa fa-bolt" style="color: #ffb400"></i> Apply for this campaign</span>
</div>
<div class="panel-body">
<%= form_for([@campaign, @campaign.contents.new]) do |f| %>
<div class="row">
<div class="col-md-12">
<label>Post Copy</label>
<%= f.text_area :post_copy, rows: 5, placeholder: "Create your post exactly as you'd like to see it published.", class: "form-control", required: true %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<label>Price USD</label>
<%= f.text_field :price, placeholder: "How much you will charge", class: "form-control", required: true %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<span class="btn btn-default btn-file text-babu">
<i class="fa fa-cloud-upload" aria-hidden="true"></i> Select Photos
<%= f.file_field "images[]", type: :file, multiple: true %>
</span>
</div>
</div>
<div class="row">
<div class="col-md-12">
<label>Where will you post?</label>
</div>
<% if !@campaign.facebook.blank? %>
<div class="col-md-3">
<%= f.check_box :is_facebook %> Facebook
</div>
<% end %>
<% if !@campaign.twitter.blank? %>
<div class="col-md-3">
<%= f.check_box :is_twitter %> Twitter
</div>
<% end %>
<% if !@campaign.instagram.blank? %>
<div class="col-md-3">
<%= f.check_box :is_instagram %> Instagram
</div>
<% end %>
<% if !@campaign.youtube.blank? %>
<div class="col-md-3">
<%= f.check_box :is_youtube %> Youtube
</div>
<% end %>
</div>
<br/>
<%= f.submit "Submit for Approval", class: "btn btn-normal btn-block" %>
<% end %>
</div>
</div>
终端响应
Started POST "/campaigns/1/contents" for 127.0.0.1 at 2018-10-18 12:46:35 +0530
Processing by ContentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"SavnYHjl5BHdUQXu4hVeyKHGgTgz7bfxXavDG8mKO2S1W0vfvjDn9RptHGJSBin/a0Gc2b7AfgzLY2eq2OpIRA==", "content"=>{"post_copy"=>"Test 5", "price"=>"10", "images"=>[], "is_facebook"=>"1", "is_instagram"=>"1"}, "commit"=>"Submit for Approval", "campaign_id"=>"1"}
Campaign Load (0.3ms) SELECT "campaigns".* FROM "campaigns" WHERE "campaigns"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/contents_controller.rb:126
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/controllers/contents_controller.rb:25
CACHE Campaign Load (0.0ms) SELECT "campaigns".* FROM "campaigns" WHERE "campaigns"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/contents_controller.rb:29
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/contents_controller.rb:32
Unpermitted parameter: :images
(0.1ms) begin transaction
↳ app/controllers/contents_controller.rb:39
Content Create (3.2ms) INSERT INTO "contents" ("user_id", "campaign_id", "post_copy", "is_facebook", "is_instagram", "price", "transaction_fee", "total", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["user_id", 2], ["campaign_id", 1], ["post_copy", "Test 5"], ["is_facebook", 1], ["is_instagram", 1], ["price", 10], ["transaction_fee", 1], ["total", 11], ["created_at", "2018-10-18 07:16:35.409569"], ["updated_at", "2018-10-18 07:16:35.409569"]]
↳ app/controllers/contents_controller.rb:39
(11.4ms) commit transaction
↳ app/controllers/contents_controller.rb:39
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/models/content.rb:16
(0.1ms) begin transaction
↳ app/models/content.rb:18
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/models/content.rb:18
Notification Create (0.8ms) INSERT INTO "notifications" ("content", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["content", "New Request from Influencer"], ["user_id", 1], ["created_at", "2018-10-18 07:16:35.443967"], ["updated_at", "2018-10-18 07:16:35.443967"]]
↳ app/models/content.rb:18
(3.1ms) commit transaction
↳ app/models/content.rb:18
[ActiveJob] Enqueued NotificationJob (Job ID: dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5) to Async(default) with arguments: #<GlobalID:0x00007fbe15387ec8 @uri=#<URI::GID gid://Notification/5>>
Redirected to http://localhost:3000/campaigns/1
Completed 302 Found in 60ms (ActiveRecord: 20.1ms)
Notification Load (0.7ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
↳ /Users/peeyushverma/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
[ActiveJob] [NotificationJob] [dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5] Performing NotificationJob (Job ID: dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5) from Async(default) with arguments: #<GlobalID:0x00007fbe15346478 @uri=#<URI::GID gid://Notification/5>>
Started GET "/campaigns/1" for 127.0.0.1 at 2018-10-18 12:46:35 +0530
[ActiveJob] [NotificationJob] [dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5] User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
[ActiveJob] [NotificationJob] [dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5] ↳ app/jobs/notification_job.rb:5
[ActiveJob] [NotificationJob] [dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5] User Update All (1.8ms) UPDATE "users" SET "unread" = COALESCE("unread", 0) + 1 WHERE "users"."id" = ? [["id", 1]]
[ActiveJob] [NotificationJob] [dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5] ↳ app/jobs/notification_job.rb:5
Processing by CampaignsController#show as HTML
Parameters: {"id"=>"1"}
Campaign Load (7.5ms) SELECT "campaigns".* FROM "campaigns" WHERE "campaigns"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
[ActiveJob] [NotificationJob] [dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5] Rendered notifications/_notification.html.erb (1.7ms)
↳ app/controllers/campaigns_controller.rb:69
[ActiveJob] [NotificationJob] [dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5] [ActionCable] Broadcasting to notification_1: {:message=>"<div class=\"panel\">\n <div class=\"panel-body\">\n <strong>New Request from Influencer</strong>\n <span class=\"pull-right\">18 Oct 07:16</span>\n </div>\n</div>\n", :unread=>2}
[ActiveJob] [NotificationJob] [dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5] Performed NotificationJob (Job ID: dd4f41ce-9428-49b5-9d29-8ec67c2bb4a5) from Async(default) in 76.89ms
NotificationsChannel transmitting {"message"=>"<div class=\"panel\">\n <div class=\"panel-body\">\n <strong>New Request from Influencer</strong>\n <span class=\"pull-right\">18 Oct 07:16</span>\n </div>\n</div>\n", "unread"=>2} (via streamed from notification_1)
Rendering campaigns/show.html.erb within layouts/application
Photo Load (1.1ms) SELECT "photos".* FROM "photos" WHERE "photos"."campaign_id" = ? [["campaign_id", 1]]
↳ app/models/campaign.rb:19
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/views/campaigns/show.html.erb:20
Campaign Load (0.8ms) SELECT campaigns.*, (111.19492664455873 * ABS(campaigns.latitude - 22.3511148) * 0.7071067811865475) + (96.29763124613503 * ABS(campaigns.longitude - 78.6677428) * 0.7071067811865475) AS distance, CASE WHEN (campaigns.latitude >= 22.3511148 AND campaigns.longitude >= 78.6677428) THEN 45.0 WHEN (campaigns.latitude < 22.3511148 AND campaigns.longitude >= 78.6677428) THEN 135.0 WHEN (campaigns.latitude < 22.3511148 AND campaigns.longitude < 78.6677428) THEN 225.0 WHEN (campaigns.latitude >= 22.3511148 AND campaigns.longitude < 78.6677428) THEN 315.0 END AS bearing FROM "campaigns" WHERE (campaigns.latitude BETWEEN 22.26118263940813 AND 22.441046960591873 AND campaigns.longitude BETWEEN 78.57050526395783 AND 78.76498033604217 AND campaigns.id != 1) ORDER BY distance ASC LIMIT ? [["LIMIT", 3]]
↳ app/views/campaigns/show.html.erb:149
Rendered contents/_form.html.erb (7.1ms)
Rendered campaigns/show.html.erb within layouts/application (32.6ms)
Rendered shared/_message.html.erb (1.2ms)
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/views/shared/_navbar.html.erb:23
Rendered shared/_navbar.html.erb (7.9ms)
Completed 200 OK in 378ms (Views: 326.5ms | ActiveRecord: 10.6ms)
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-10-18 12:46:35 +0530
MessagesChannel stopped streaming from conversation_
NotificationsChannel stopped streaming from notification_2
Started GET "/cable" for 127.0.0.1 at 2018-10-18 12:46:36 +0530
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-10-18 12:46:36 +0530
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
MessagesChannel is transmitting the subscription confirmation
MessagesChannel is streaming from conversation_
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notification_2
答案 0 :(得分:0)
要支持paperclip上传到s3,您需要将gem "aws-sdk-s3"添加到Gemfile文件吗?并在模型中设置s3_credentials
class Content < ApplicationRecord
#
# rest of codes
###
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" } ,
:storage => :s3,
:s3_credentials => {
:bucket => "xxx",
:access_key_id => "xxx",
:secret_access_key => "xxx"}
#rest of code
end
仅供参考https://www.rubydoc.info/gems/paperclip/Paperclip/Storage/S3
答案 1 :(得分:0)
在您的模型中,您具有:
has_attached_file :image
因此,我假设,每个内容都仅附有一个图像,但是在视图的表单中,您正在将多个图像作为数组上传。
<%= f.file_field "images[]", type: :file, multiple: true %>
将此更改为:
<%= f.file_field :image %>
此外,在您的ContentController中,在content_params动作中,将:images参数更改为:image
params.require(:content).permit(:post_copy, :is_facebook, :is_instagram, :is_twitter, :is_youtube, :price, :image)
但是,如果要附加多张图像,则必须完全更改方法。