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

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

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

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

0回答

607閲覧

複数の画像を投稿することができない

kimaiio

総合スコア8

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

1グッド

1クリップ

投稿2020/04/12 11:07

Ruby on Railsで画像を投稿できる機能を作ったのですが複数の画像が投稿できません。
一枚だけなら投稿できるのですがmultiple: trueを付け複数枚を選択して投稿すると一枚も投稿されなくなってしまいます。

posts_control.rb

Ruby

1 def new 2 @post = Post.new 3 end 4 def create 5 @post = Post.new(post_params) 6 if @post.save 7 8 flash[:notice] = "投稿を送信しました。" 9 redirect_back(fallback_location: root_path) 10 else 11 redirect_to new_post_path 12 flash[:alert] = "投稿に失敗しました。" 13 end 14 end

posts.rb

class Post < ApplicationRecord default_scope -> { order(created_at: :desc) } belongs_to :user validates :text, length: { maximum: 1200 } mount_uploader :image, ImageUploader mount_uploader :video, VideoUploader has_many :comments, dependent: :destroy has_many :notifications, dependent: :destroy has_many :likes, -> { order(created_at: :desc) }, dependent: :destroy has_many :downs, -> { order(created_at: :desc) }, dependent: :destroy def liked_by(user) Like.find_by(user_id: user.id, post_id: id) end def downd_by(user) Down.find_by(user_id: user.id, post_id: id) end def create_notification_like!(current_user) temp = Notification.where(["visitor_id = ? and visited_id = ? and post_id = ? and action = ? ", current_user.id, user_id, id, 'like']) if temp.blank? notification = current_user.active_notifications.new( post_id: id, visited_id: user_id, action: 'like' ) if notification.visitor_id == notification.visited_id notification.checked = true end notification.save if notification.valid? end end def self.search(search) if search Post.where(['text LIKE ?', "%#{search}%"]) else Post.all end end def create_notification_comment!(current_user, comment_id) temp_ids = Comment.select(:user_id).where(post_id: id).where.not(user_id: current_user.id).distinct temp_ids.each do |temp_id| save_notification_comment!(current_user, comment_id, temp_id['user_id']) end save_notification_comment!(current_user, comment_id, user_id) if temp_ids.blank? end def save_notification_comment!(current_user, comment_id, visited_id) notification = current_user.active_notifications.new( post_id: id, comment_id: comment_id, visited_id: visited_id, action: 'comment' ) if notification.visitor_id == notification.visited_id notification.checked = true end notification.save if notification.valid? end end

new.html.erb

<div class="timeline-post-new" id="form-post-<%= current_user.id.to_s %>"> <%= form_with model: @post do |f| %> <%= f.text_area :text, placeholder: "投稿内容", class: "timeline-post-form-new", rows: "3",onkeyup: "ShowLength(value);",id: "text" %> <div class="timeline-new-buttons"> <div id="preview" class="preview"></div> <%= f.file_field :image, id: "filesend",onChange: "imgPreView(event)",accept: "image/*",class: <%= f.submit :投稿, value: "投稿", class: "new-post-btn",id: "submit" %> <p id="inputlength" class="new-textlength"><span id="messageCount">0</span>/1200</p> </div> <% if current_user.high == true %> <div class="accbox"> <label for="label1"><span class="material-icons drop-down">arrow_drop_down</span></label> </div> <% end %> <% end %> </div>

※コードが多すぎたので不要だと思われるコード(この件に関係ない部分)は省略しています。

複数の画像を投稿する場合 file_field にmultiple: trueをつけるだけではいけないのでしょうか。
詳しい方教えていただけると幸いです。

iio👍を押しています

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

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

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

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

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

no1knows

2020/04/12 16:21

利用しているのはcarrierwaveでしょうか?アップローダーをきちんと明記しておいたほうが回答がつきやすくなるかと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問