0

jQuery File Upload と Rails 4 で問題が発生しています。1 つのことを除いて、すべてが機能しているようです。 ファイルが正常にアップロードされ、データがデータベースに正常に投稿されました。しかし、アップロードまたは「作成」時に次のエラーが発生します。

 SyntaxError: Unexpected token $

私の推測: JSON を受け取りたいということと関係があると思いますが、これを達成する方法やどこに置くべきかわかりません。どんな助けでも素晴らしいでしょう!

ここに私が持っているものがあります:

create.js.erb

<% if @choice.new_record? %>
   alert("Failed to upload image: <%= j @choice.errors.full_messages.join(', ').html_safe      %>");
 <% else %>
  $("#paintings").append("<%= j render(@choice) %>");
 <% end %>

index.html.erb

  <h1>Choices Gallery</h1>

<div id="paintings">
  <%= render @choices %>
</div>
<div class="clear"></div>

<div class="container">
  <h2>Upload file</h2>
  <%= form_for Choice.new, :html => { :multipart => true, :id => "fileupload"  } do |f| %>
<!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
<div class="row fileupload-buttonbar">
  <div class="span7">
    <!-- The fileinput-button span is used to style the file input field as button -->
    <span class="btn btn-success fileinput-button">
      <i class="icon-plus icon-white"></i>
      <span>Add files...</span>
      <%= f.file_field :image, multiple: true, name: "choice[image]" %>
    </span>
    <button type="submit" class="btn btn-primary start">
      <i class="icon-upload icon-white"></i>
      <span>Start upload</span>
    </button>
    <button type="reset" class="btn btn-warning cancel">
      <i class="icon-ban-circle icon-white"></i>
      <span>Cancel upload</span>
    </button>
    <button type="button" class="btn btn-danger delete">
      <i class="icon-trash icon-white"></i>
      <span>Delete</span>
    </button>
    <input type="checkbox" class="toggle">
  </div>
  <div class="span5">
    <!-- The global progress bar -->
    <div class="progress progress-success progress-striped active fade">
      <div class="bar" style="width:0%;"></div>
    </div>
  </div>
</div>
<!-- The loading indicator is shown during image processing -->
<div class="fileupload-loading"></div>
<br>
<!-- The table listing the files available for upload/download -->
<table class="table table-striped"><tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody>
</table>

choices_controller.rb

 class ChoicesController < ApplicationController
def index
  @choices = Choice.all
end

def show
  @choice = Choice.find(params[:id])
end

def new
  @choice = Choice.new
end

def create
  @choice = Choice.create(choice_params)
end
end

更新 #1 まず、CarrierWave を使用していることを忘れていました。第二に、コンソールが返すものは次のとおりです(問題なくDBに追加されることがわかります。アップロードした後にエラーが発生するようです)。

Started POST "/choices" for 127.0.0.1 at 2013-08-15 15:07:45 -0700
Processing by ChoicesController#create as JSON
Parameters: {"utf8"=>"✓",     "authenticity_token"=>"iKxDDhTAbu/hY/QqEk9jXr9NJltIQmGWu6tUZEMe7pM=", "choice"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fe3f2c50818 @tempfile=#<Tempfile:/var/folders/z6/c199lpm13492b758r5xx0ngm0000gn/T/RackMultipart20130815-28015-ngm6w2>, @original_filename="speech_teacher.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"choice[image]\"; filename=\"speech_teacher.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
(0.2ms)  BEGIN
SQL (26.4ms)  INSERT INTO `choices` (`created_at`, `image`, `name`, `updated_at`) VALUES ('2013-08-15 22:07:45', 'speech_teacher.jpg', 'Speech Teacher', '2013-08-15 22:07:45')
(0.4ms)  COMMIT
Redirected to http://localhost:3000/choices
Completed 302 Found in 75ms (ActiveRecord: 27.0ms)


Started GET "/choices" for 127.0.0.1 at 2013-08-15 15:07:45 -0700
Processing by ChoicesController#index as JSON
Choice Load (1.7ms)  SELECT `choices`.* FROM `choices`
Rendered choices/_choice.html.erb (2.5ms)
Rendered choices/index.html.erb within layouts/application (13.0ms)
Completed 200 OK in 109ms (Views: 106.2ms | ActiveRecord: 1.7ms)
4

0 に答える 0