質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

4631閲覧

【rails】form_forでremote:trueにしているのに、HTMLでリクエストされてしまう。

gogoackman3

総合スコア109

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

2クリップ

投稿2016/11/07 14:31

編集2016/11/07 15:38

ゲーム画像を投稿するフォームを作成しています。

投稿項目は、①画像と②テキストと③ゲーム名の三つです。

ajaxでの実装にしたのですが、なぜか画像ありの場合だけ、HTMLでリクエストがされてしまいます。
環境:Rails 5.0.0.1

フォームとコントローラなどは以下の通りです。

▼フォーム(view)

ruby

1 = form_for(@post,html:{id:'post_form', class:'post_form_wrap', multipart: true}, remote: true) do |f| 2 = render 'shared/error_messages', object: f.object 3 .post_game 4 p.selected_game.title_field 5 = f.collection_select :game_id, current_user.following_games.all, :id, :title, prompt: 'ゲームを選択してください' 6 7 .post_image 8 p.selected_image.title_field 9 div.camera_wrap 10 = image_tag('camera.png',alt:'投稿',id:'camera_image') 11 = f.file_field :image, accept: 'image/jpeg,image/png,image/gif',id: 'image_item' 12 13 = f.hidden_field :image_cache 14 - if @post.image? 15 = image_tag @post.image.url ,id: 'post_image_data' 16 - else 17 p#preview No Image 18 19 .post_text 20 p.comment.title_field.clearfix 21 span#comment_chara_count.chara_count 22 = f.text_area :text, class: 'post_form text-counter', placeholder: 'コメント',data: { 'text-counter-tag': '#comment_chara_count', 'text-counter-max': 140 },rows: 4 23 24 ul#js_image_error.input_error 25 .post_submit 26 = f.submit '投稿',id: 'post_submit_btn'

▼controller

ruby

1def create 2 @post = current_user.posts.build(post_params) 3 if @post.save 4 @success = true 5 else 6 @success = false 7 end 8 render 9 end

▼create.js.slim

ruby

1- if @success == true 2 | window.location.href = "/"; 3- else 4 | $('#error_explanation').remove(); 5 | $('.post_body').prepend(" #{ j(render 'shared/error_messages', object: @post )}");

▼画像を未選択でsubmitした場合のログ(問題なく、JS形式でリクエストされます。)

Started POST "/posts" for 10.0.2.2 at 2016-11-07 23:17:40 +0900 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by PostsController#create as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"lZ7HUoMQ4QgWffdON8KNX/PlZaKlVjg4cxO4qvCqKdHG9KMVlzMgFAwjrX1JGL9pOFpOIWdix1WZeMladvtOew==", "post"=>{"game_id"=>"", "image_cache"=>"", "text"=>"testです"}, "commit"=>"投稿"} User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 11 ORDER BY `users`.`id` ASC LIMIT 1 (0.1ms) BEGIN (0.1ms) ROLLBACK Rendering posts/create.js.slim Rendered shared/_error_messages.html.slim (3.1ms) Rendered posts/create.js.slim (8.9ms) Completed 200 OK in 25ms (Views: 15.0ms | ActiveRecord: 0.8ms)

▼何らかの画像を選択した状態でsubmitすると・・・なぜかHTML形式のリクエストに・・・

Started POST "/posts" for 10.0.2.2 at 2016-11-07 23:17:57 +0900 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"lZ7HUoMQ4QgWffdON8KNX/PlZaKlVjg4cxO4qvCqKdHG9KMVlzMgFAwjrX1JGL9pOFpOIWdix1WZeMladvtOew==", "post"=>{"game_id"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x007f0c83af3098 @tempfile=#<Tempfile:/tmp/RackMultipart20161107-3362-vcr1e5.PNG>, @original_filename="20161005233954-1021557906.PNG", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"20161005233954-1021557906.PNG\"\r\nContent-Type: image/png\r\n">, "image_cache"=>"", "text"=>""}, "commit"=>"投稿"} User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 11 ORDER BY `users`.`id` ASC LIMIT 1 (0.1ms) BEGIN (0.1ms) ROLLBACK Completed 500 Internal Server Error in 12ms (ActiveRecord: 0.5ms) ActionView::MissingTemplate (Missing template posts/create, application/create with {:locale=>[:ja], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :slim, :coffee, :jbuilder]}. Searched in: * "/home/vagrant/gameparty/app/views" * "/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/kaminari-0.17.0/app/views" * "/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-i18n-1.1.0/app/views" * "/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.2.0/app/views" ):

全てがHTML形式になってしまうのであれば、分かるのですが、画像を選択した場合だけなのが全く意味がわかりません。

打開策として、formに「format:'js'」と追記したところ、画像無しの場合は引き続き、問題なく、画像ありの場合は以下のjsが描画されてしまいます。

イメージ説明

どなたか、この事象の発生要因などがわかりませんでしょうか??

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

railsの仕様?のようでした。

以下の方法で解決しました。

http://nanofi.hatenablog.com/entry/20111226/1324851176

投稿2016/11/25 05:42

gogoackman3

総合スコア109

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問