Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails4上传完成后通过输入,然后如何触发引导模式?_Ruby On Rails_Bootstrap Modal - Fatal编程技术网

Ruby on rails Rails4上传完成后通过输入,然后如何触发引导模式?

Ruby on rails Rails4上传完成后通过输入,然后如何触发引导模式?,ruby-on-rails,bootstrap-modal,Ruby On Rails,Bootstrap Modal,我的档案: gem 'rails', '4.2.8' gem 'carrierwave', '~> 1.2', '>= 1.2.2' gem 'mini_magick', '~> 4.8' gem 'Jcrop', '~> 0.1.0' 现在我想使用form_来上传用户图片,myshow.html.erb: <%= form_for @user, :html => {:multipart => true} do |f| %> <

我的档案:

gem 'rails', '4.2.8'
gem 'carrierwave', '~> 1.2', '>= 1.2.2'
gem 'mini_magick', '~> 4.8'
gem 'Jcrop', '~> 0.1.0'
现在我想使用
form_来上传用户图片,my
show.html.erb

    <%= form_for @user, :html => {:multipart => true} do |f| %>
  <div class="row " id="user_avatar_crop">
    <!-- Choose Image -->
    <div class="col-md-12">
      <%= f.file_field :picture, id: :user_avatar, onchange: 'this.form.submit()'%>
    </div>
  </div>
<% end %>

<!-- Modal -->
<div id="uploadModalContent">

</div>

<!-- Show user picture -->
<% if @user.picture? %>
  <%= image_tag @user.picture.url(:thumb), :alt => @user.name+"_avatar" %>
  <% else %>
  <%= image_tag @user.picture.url(:thumb),:alt => @user.name+"_default" %>
  <% end %>
$('#uploadModalContent').html("<%= j render "users/modal"%>");
$('#upload-modal').modal('show');
<div class="modal fade" id="upload-modal" aria-labelledby="modalLabel" role="dialog" tabindex="-1">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4 class="modal-title">Crop Image</h4>
      </div>
      <div class="modal-body">
        <div class="col-md-8">
          <%= image_tag @user.picture_url(:large), id: "cropImage" %>
        </div>
        <div class="col-md-4">
          <h4>Preview</h4>
          <div style="width:100px; height:100px; overflow:hidden;">
            <%= image_tag @user.picture.url(:large), :id => "user_preview" %>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <%= form_for @user do |f| %>
          <% %w[x y w h].each do |attribute| %>
            <%= f.hidden_field "crop_#{attribute}" %>
          <% end %>
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <%= f.submit "crop" %>
        <% end %>
      </div>
    </div>
  </div>
</div>
My
update.js.erb

    <%= form_for @user, :html => {:multipart => true} do |f| %>
  <div class="row " id="user_avatar_crop">
    <!-- Choose Image -->
    <div class="col-md-12">
      <%= f.file_field :picture, id: :user_avatar, onchange: 'this.form.submit()'%>
    </div>
  </div>
<% end %>

<!-- Modal -->
<div id="uploadModalContent">

</div>

<!-- Show user picture -->
<% if @user.picture? %>
  <%= image_tag @user.picture.url(:thumb), :alt => @user.name+"_avatar" %>
  <% else %>
  <%= image_tag @user.picture.url(:thumb),:alt => @user.name+"_default" %>
  <% end %>
$('#uploadModalContent').html("<%= j render "users/modal"%>");
$('#upload-modal').modal('show');
<div class="modal fade" id="upload-modal" aria-labelledby="modalLabel" role="dialog" tabindex="-1">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4 class="modal-title">Crop Image</h4>
      </div>
      <div class="modal-body">
        <div class="col-md-8">
          <%= image_tag @user.picture_url(:large), id: "cropImage" %>
        </div>
        <div class="col-md-4">
          <h4>Preview</h4>
          <div style="width:100px; height:100px; overflow:hidden;">
            <%= image_tag @user.picture.url(:large), :id => "user_preview" %>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <%= form_for @user do |f| %>
          <% %w[x y w h].each do |attribute| %>
            <%= f.hidden_field "crop_#{attribute}" %>
          <% end %>
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <%= f.submit "crop" %>
        <% end %>
      </div>
    </div>
  </div>
</div>
现在我需要上传图片后,
\u modal.html.erb
可以显示。但是
user\u controller.rb
中的
format.js{render template:'users/update.js.erb'}
似乎无法工作。这是为什么


user\u controller.rb
中,在
input
complete
onchange:'this.form.submit()'
之后,我应该怎么做才能将它呈现到模式窗口?非常感谢您的帮助。

我找到了另一种在rails中上传图像的方法。我得出的结论是,这是迄今为止我所知道的最好的方法。你必须使用gem。我现在将输入使用它所需的代码。无论如何,如果你能检查github回购协议或这个

好吧,我们走吧。您必须首先在全球范围内安装gem,甚至在您的项目中本地安装

$ gem install carrierwave
在Rails中,将其添加到文件:

gem 'carrierwave', '~> 1.0'
现在重新启动服务器以应用更改

首先生成一个上传程序:

rails generate uploader Photos
这将为您提供以下格式的文件:

# app/uploaders/photos_uploader.rb
class PhotosUploader < CarrierWave::Uploader::Base
  storage :file
  # will save photos in /app/public/uploads
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

好了,就这些。让我知道进展如何

非常感谢你的回答,我已经尝试了你的代码,但是有一些问题。1、
ActionController::InvalidAuthenticityToken
,我通过将
config.action\u controller.allow\u forgery\u protection=false
添加到
config\environments\development.rb
中解决了这个问题,然后它显示了另一个错误,
模板丢失
模板用户丢失/更新…
,如何修复此问题?再次感谢。是的,@neboduus,我在
views/users
中找到了
update.js.erb
。哦,它显示了错误:`ActionController::UnknownFormat in userscocontroller#update
,我在
@template\u missing?更改了你的代码对于
@template\u missing
,对于您的代码
@template\u missing?
,我的rubyMine显示错误:
预期:表达式
。这有关系吗@整个错误是
缺少模板用户/更新,应用程序/更新为{:locale=>[:en],:formats=>[:html],:variants=>[],:handlers=>[:erb,:builder,:raw,:ruby,:arb,:coffee,:jbuilder]}。搜索地址:“/Users/hs/Desktop/uask/app/views”*“/Users/hs/.rvm/gems/ruby-2.5.0/gems/designe-4.4.3/app/views”*“/Users/hs/.rvm/gems/ruby-2.5.0/gems/activeadmin-1.3.0/app/views”*“/Users/hs/.rvm/gems/gems/ruby-2.5.0/gems/kaminari-core-1.1.1/app/views”
,我发现,如果我这样做,我会问:
https://stackoverflow.com/questions/37819575/how-to-show-modal-window-in-controllers-action
Zelenka的第二个答案。如果您的代码设置为
@template_missing
,然后我在我的
show.html.erb
中使用js调用这个变量val,并判断值为true,最后我可以使用
$(“#upload”).modal(“show”)
,这个想法能实现吗?
class PhotosController < ApplicationController
  def index
    @photos = Photo.all
  end
  def show
    @photo = Photo.find(params[:id])
  end
  def new
    @photo = Photo.new
  end
  def create
    # build a photo and pass it into a block to set other attributes
    @photo = Photo.new(photo_params)
    # normal save
    if @photo.save
      redirect_to(@photo, :notice => 'Photo was successfully created.')
    else
      render :action => "new"
    end
  end
  private
    def photo_params
      params.require(:photo).permit!
    end
end
<!-- new.html.erb -->
<%= form_for(@photo, :html => {:multipart => true}) do |f| %>
  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :data %>
    <%= f.file_field :data %>
  </div>
  <div class="actions">
    <%= f.submit "Upload" %>
  </div>
<% end %>
<!-- show.html.erb -->
<h3>Photo: <b><%= @photo.name %></b></h3>
<%= image_tag @photo.data.url %>
# app/assets/javascripts/photos.coffee
$ ->
  alert('Photo Uploaded - You can launch modal here')