1

I used

gem 'rails', '4.2.8'
gem 'carrierwave', '~> 1.2', '>= 1.2.2'
gem 'mini_magick', '~> 4.8'
gem 'Jcrop', '~> 0.1.0'

and then I write the code as it https://www.driftingruby.com/episodes/cropping-images-with-jcrop

It works good.

But the problem is when I want to change the crop.html.erb to use Bootstrap _modal.html.erb, How should change the user_controller.erb?

the original codes is like this :

def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      if params[:user][:picture].present?
        render :crop   ###this how to change use modal.html
      else
        redirect_to @user
        flash[:success] = "更新成功"
      end
    else
      render :edit
    end
  end

I had try as

https://stackoverflow.com/questions/37819575/how-to-show-modal-window-in-controllers-action?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

And the new user_controller.erb

def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      if params[:user][:picture].present?
        respond_to do |format|  ##change here
          format.js    ##change here
        end   ##change here
      else
        redirect_to @user
        flash[:success] = "更新成功"
      end
    else
      render :edit
    end
  end

But it always show that :

ActionController::UnknownFormat

If add format.html ,it has wrongs templates missing, because I write the html codes in show.html.erb,it likes:

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

  </div>
  <!-- Modal -->
<% end %>
<div class="userAvatarUpload">

</div>
<%= image_tag @user.picture_url(:thumb) if @user.picture? %>

How should I format.html to show.html.erb ?

My questions just like this:

https://stackoverflow.com/questions/21027034/rails-carrierwave-jcrop-bootstrap

Should I just had to use paperclip like him? Is there any method to solve this problem?

Hardik Upadhyay
  • 2,639
  • 1
  • 19
  • 34
SylorHuang
  • 313
  • 4
  • 12

1 Answers1

0
if params[:user][:picture].present?
    render :crop   ###this how to change use modal.html
  else

Whenever you use render, you're rendering an action, not an HTML page. My advice would be to just create a new action with the name bootstrap _modal (do not use Bootstrap with a capital B) and rename your Bootstrap _modal.html.erb to bootstrap _modal.html.erb.

a3y3
  • 1,025
  • 2
  • 10
  • 34
  • Thanks @a3y3. I want to pop the `modal` in current page, if I render to an action , will it jump to a new page? And the `modal` display should use JS `$('#xxx').modal("show")`,how could `controller` `render` to call the JS file? – SylorHuang May 17 '18 at 14:04
  • Hi @a3y3,in this question , the second answer by `@Zelenka` seems can achieve the result, but how could set the variable, can you give me some suggests? The question link:`https://stackoverflow.com/questions/37819575/how-to-show-modal-window-in-controllers-action` – SylorHuang May 17 '18 at 14:12