#前提・実現したいこと
Rails 5 でユーザー投稿型サービスを開発しています。
投稿作成画面で、ユーザーが選択した画像を自由にトリミングした上で投稿できるようにしたいです。
以下の記事を参考に作っています。
CarrierwaveとMiniMagickでユーザー任意の入力値で画像をトリミングする
#発生している問題
ユーザーに画像を選択し、トリミングさせるところまでは実装できたのですが、そこからsubmitボタンを押しても、formがcreateアクションに渡されず、現在いるビューが再描画されるだけになっており、悩んでいます。
#該当のソースコード
Ruby
1#new.html.erb 2<div class="main posts-new"> 3 <div class="container"> 4 5 <h1 class="form-heading">新規投稿</h1> 6 7 <form> 8 <div class="form"> 9 <div class="form-body"> 10 11 <% @post.errors.full_messages.each do |message| %> 12 <div class="form-error"> 13 <%= message %> 14 </div> 15 <% end %> 16 17 <%= form_for @post, :url => { :controller => :posts, :action => :create } do |f|%> 18 <%= f.label("アイテム画像(必須)") %> 19 <%= f.file_field :image_name, multiple: true , :id => "upload"%> 20 <%= f.hidden_field :image_x %> 21 <%= f.hidden_field :image_y %> 22 <%= f.hidden_field :image_w %> 23 <%= f.hidden_field :image_h %> 24 <div class="preview"><img id="crop_img" /></div> 25 <div class="clear_float"></div> 26 27 <%= f.hidden_field :designer_id, :value => @current_user.id %> 28 <div class="clear_float"></div> 29 30 <%= f.label("コメント") %> 31 <%= f.text_area :detail, :placeholder => "アイテムに関するこだわりなどあれば、自由に入力してください"%> 32 33 <%= f.submit("投稿", :id => "submit") %> 34 <% end %> 35 36 </div> 37 </div> 38 </form> 39 </div> 40 41 <%= stylesheet_link_tag 'cropper.min.css' %> 42 <%= javascript_include_tag 'cropper.min.js' %> 43 <%= javascript_include_tag 'crop_image.js' %> 44</div>
javascript
1... 2$('#submit').on('click', function(){ 3 var _form = $("#create_posts"); 4 // crop のデータを取得 5 var data = $('#crop_img').cropper('getData'); 6 7 $("#post_image_x").val(Math.round(data.x)); 8 $("#post_image_y").val(Math.round(data.y)); 9 $("#post_image_w").val(Math.round(data.width)); 10 $("#post_image_h").val(Math.round(data.height)); 11 12 _form.submit(); 13 14});
Ruby
1class PostsController < ApplicationController 2... 3 def new 4 @post = Post.new 5 end 6 7 def create 8 post = params.require(:post).permit( 9 :designer_id, 10 {image_name: []}, 11 :image_x, 12 :image_y, 13 :image_w, 14 :image_h, 15 :detail 16 ) 17 @post = Post.new(post) 18 if @post.save 19 flash[:notice]="投稿しました" 20 redirect_to("/") 21 NotificationMailer.post_email(@current_user, @post).deliver 22 else 23 render("posts/new") 24 end 25 end 26... 27end
#使用している環境
Rails 5.1.5
carrierwaves 1.2.2
Cropper.js
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。