无法通过回形针在滑轨5上载图像

时间:2018-10-17 13:49:40

标签: ruby-on-rails ruby

错误

Error while uploading the image

我无法上传嵌套脚手架形式的图像。

除图像外,所有其他字段都进入数据库。请帮忙!

我在5.2上,使用回形针进行图像上传,并且需要将图像上传到AWS。

我没有任何错误,只是图像没有进入数据库。

谢谢!

部分表单

tempate<class R, class...Args>
void bar( std::function<R(Args...)> f ) {}
template<class F>
void bar( F f ) {
  std::function std_f = std::move(f);
  bar(std_f);
}

控制器

    <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 "image[]", 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>

Content.rb

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, image: [])
  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

1 个答案:

答案 0 :(得分:1)

  

不允许的参数::images

您的代码需要进行一些重要的更改。

1)这个

<%= file_field_tag "images[]", type: :file, multiple: true %>

应该是

<%= f.file_field "image[]", type: :file, multiple: true %>

2)您需要修改content_params,以允许图片接受值数组。在:image

中将image: []更改为content_params