简体   繁体   English

Remotipart(+ Carrierwave)不上传文件也不使用Ajax

[英]Remotipart ( + Carrierwave) not uploading files nor using ajax

I've spent a week right now trying to get this to work, reading as much as I can about remotipart, and trying to set it right, but I have failed miserably. 我现在已经花了一个星期的时间来尝试使它正常工作,我尽可能多地阅读有关remotipart的内容,并尝试将其设置为正确,但是我失败了。 I have form which has a title, description, an audio file and an image file. 我的表格有标题,描述,音频文件和图像文件。 If I submit the form without remote: true it works perfectly, but once I try to upload the form with ajax, it seems like the files are not being uploaded. 如果我不使用remote: true提交表单remote: true可以正常工作,但是一旦我尝试使用ajax上载表单,似乎文件就没有上载。

Since I made the audio file a requirement for posting, I get redirected to the new action, displaying the error indicating that 'audio' must not be blank. 由于我将音频文件作为发布的要求,因此我被重定向到新操作,显示错误消息,指示“音频”不能为空。

Even if I remove this validation from the model, once I try to upload, there is no audio file being uploaded. 即使我从模型中删除了此验证,一旦尝试上载,也就没有音频文件被上载。

Also, by checking the developer tools, I've realized that the response I'm getting is not javascript, but html. 此外,通过检查开发人员工具,我意识到我得到的响应不是javascript,而是html。

I've already tried other gems, like jquery-uploadfile-rails , but none of them work for me for different reasons. 我已经尝试过其他宝石,例如jquery-uploadfile-rails ,但是由于不同的原因,它们都不适合我。 I have no idea what to do right now. 我不知道现在该怎么办。

_form.html.erb _form.html.erb

<%= form_for(post, html: {multipart: true, remote: true}, authenticity_token: true) do |f| %>

<div id="form-content">
    <div class="input-group field">
        <span class="input-group-addon"><%= f.label 'Title' %></span>
        <%= f.text_field :title, class: "form-control", placeholder: "Title" %>
    </div>

    <div class="input-group field">
        <span class="input-group-addon"><%= f.label :descrption %></span>
        <%= f.text_field :description, class: "form-control", placeholder: "Brief Description" %>
    </div>

    <div class="input-group field">
        <span class="input-group-addon"><%= f.label :audio %></span>
        <%= f.file_field :audio, class: "filestyle", 'data-iconName' => "glyphicon glyphicon-music", 'data-buttonText' => "Browse" %>
    </div>

    <div class="input-group field">
        <span class="input-group-addon"><%= f.label :art %></span>
        <%= f.file_field :art, class: "filestyle", 'data-iconName' => "glyphicon glyphicon-picture", 'data-buttonText' => "Browse" %>
    </div>


    <%= button_tag(type: 'submit', class: "btn btn-block btn-primary", id: "btn-post") do %>
        <span class="icon-ok icon-white"></span> Post now!
    <% end %>

</div>
<% end %>

posts_controller.rb posts_controller.rb

def create
    @post = Post.new(post_params)
    @post.user_id = current_user.id
    respond_to do |format|
        if @post.save
            format.html { redirect_to action: 'index' }
            format.js
            format.json { render :show, status: :created, location: @post }
        else
            format.html { render :new }
            format.js
            format.json { render json: @post.errors, status: :unprocessable_entity }
        end
    end
end

create.js.erb create.js.erb

// Display a Javascript alert
alert('success!');
<% if remotipart_submitted? %>
    alert('submitted via remotipart')
<% else %>
    alert('submitted via native jquery-ujs')
<% end %>

Lastly, I'm still learning about rails, it's arquitechture, and the 'rails way'. 最后,我仍在学习Rails,arquitechture和“ rails way”。 Even though I've been trying to do everything correctly, I'm pretty sure I have been improvising some parts, trying to solve errors. 即使我一直在尝试正确地完成所有事情,但我可以肯定的是我已经在即兴创作某些部分,试图解决错误。 If you find anything weird and feel like sharing, I'll be completely open to learn the good way. 如果您发现任何奇怪的东西并且想分享,我将完全开放学习这种好方法。 If you need to check any other part of my code just tell me. 如果您需要检查代码的任何其他部分,请告诉我。 Thanks! 谢谢!

When using remotipart you have to specify the action in your form_for, for example - this should be 使用remotipart时,您必须在form_for中指定操作,例如-这应该是

<%= form_for(post, remote: true, format: "js", html: {multipart: true}, authenticity_token: true) do |f| %>

Let me know if this works for you! 让我知道这是否适合您! Also, I'm not sure if the authenticity_token will work here. 另外,我不确定authenticity_token是否可以在这里使用。 You may have to go into your controller to disable this with a before_action. 您可能需要进入控制器以使用before_action禁用此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM