0

我遇到了 jQuery File Upload 和 Rails 4 的问题。除了一件事,一切似乎都在工作! 文件已成功上传,数据已成功发布到数据库。但是,上传或“创建”时出现以下错误:

 SyntaxError: Unexpected token $

我的猜测: 我认为它与想要接收 JSON 回来有关,但我不知道如何实现这一点或将它放在哪里。任何帮助都会很棒!

这是我所拥有的:

创建.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>

选择控制器.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。其次,这是控制台返回的内容:(您可以看到它添加到数据库没有问题。上传后似乎有错误)。

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