Rails 5:未允许的参数:: image

时间:2018-03-14 15:39:36

标签: ruby-on-rails

我的编辑操作中有两个表单,一个是更新item.rb model内的项目(工作正常)...另一个是在attachment.rb model中创建图像。问题是它在db images内部创建了一个空值,并没有保存实际图像和正确的值。

items_contoller.rb

 def edit
  @attachment = Attachment.new
 end

这是图片创建的表单:

<%= form_for @attachment, url: create_attachment_path(@attachment), :html => {:id => "form", :multipart => true }, method: :post do |form| %>
      <% if @attachment.errors.any? %>
          <div class="centerList">
            <div id="error_explanation">
              <h2><%= pluralize(item.errors.count, "error") %> <%= t 'store_item_edit_4' %></h2>
              <% @attachment.errors.full_messages.each do |message| %>
                  <li><%= message %></li>
              <% end %>
            </div>
          </div>
      <% end %>
    <%= hidden_field_tag :item_id, value: @item.id  %>
      <div class="form-group">
        <div class="text-center">
        <label class="btn btn-primary"><%= t 'store_item_edit_5' %><span style="display:none;">
         <%= form.file_field :image, multiple: true, id: "uploads" %></span></label>
        <%= form.submit '', :style => "display: none;" %>
  <% end %>

这是路线: post "attachments/create"=> "attachments#create", :as => :create_attachment

attachments_controller.rb

def create
@attachment = Attachment.new(attachment_params)

respond_to do |format|
  if @attachment.save
    format.html { redirect_back fallback_location: root_path, notice: 'Image was successfully uploaded.' }
    format.json { render :show, status: :created, location: @attachment }
  else
    format.html { render :new }
    format.json { render json: @attachment.errors, status: :unprocessable_entity }
  end
  end
 end


def attachment_params
  params.require(:attachment).permit(:item_id, :account_id, :image)
end

这就是我进入控制台......在某些时候,你可以看到我得到了Unpermitted parameter: :image

started POST "/attachments/create" for 127.0.0.1 at 2018-03-14 17:20:23 +0200
Processing by AttachmentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"6kgGVoUf2tlM2YdxZis6xavw//zC4azttYi4FFshgw4swiFUIOfb58hCtZxf0If2ihOXz3SCETQSnci6l1IFIA==", "item_id"=>"{:value=>44}", "attachment"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x007fc6e4acfb18 @tempfile=#<Tempfile:/var/folders/hq/pr4rt14n7s31v3f6292wtjm00000gn/T/RackMultipart20180314-4193-1jrij48.jpg>, @original_filename="image1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachment[image][]\"; filename=\"image1.jpg\"\r\nContent-Type: image/jpeg\r\n">]}}
  Store Load (0.5ms)  SELECT  "stores".* FROM "stores" WHERE "stores"."id" = $1 ORDER BY "stores"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
Unpermitted parameter: :image
   (0.2ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "attachments" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", "2018-03-14 15:20:23.368574"], ["updated_at", "2018-03-14 15:20:23.368574"]]
   (1.2ms)  COMMIT
Redirected to https://localhost:3000/store/items/edit/44
Completed 302 Found in 8ms (ActiveRecord: 2.3ms)

任何想法如何解决这个问题:

1 个答案:

答案 0 :(得分:0)

您应该添加:

def attachment_params
  params.require(:attachment).permit(:item_id, :account_id, image: {})
end