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

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

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

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

Q&A

0回答

829閲覧

rails wrong number of argumentsの解決方法

hutaba2828

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2021/10/09 05:59

編集2021/10/09 06:30

前提・実現したいこと

noteに似たものを作ろうとしており、
scaffledを利用して画像の投稿機能を追加したいです。

情報が不足していれば、足すので教えてください。
素人質問で恐縮ですが、どうぞよろしくお願いいたします。

発生している問題・エラーメッセージ

wrong number of arguments (given 3, expected 2; required keyword: object) <%= f.attachment_field :image, class: "input" %>

該当のソースコード

app/controllers

notes_controller.rb

class NotesController < ApplicationController before_action :authenticate_user!, except: [:index] def new @note = Note.new end def create @note = current_user.notes.build(note_params) if @note.save redirect_to note_path(@note), notice: "ノートを投稿しました。" else render :new end end def index @notes = Note.all.order(id: "DESC") end def show @note = Note.find(params[:id]) end def edit @note = Note.find(params[:id]) if @note.user != current_user redirect_to notes_path, alert: "不正なアクセスです。" end end def update @note = Note.find(params[:id]) if @note.update(note_params) redirect_to note_path(@note), notice: "ノートを更新しました。" else render :edit end end def destroy note = Note.find(params[:id]) note.destroy redirect_to user_path(note.user), notice: "ノートを削除しました。" end private def note_params params.require(:note).permit(:title, :body, :image, :comment) end end

app/models

notes.rb

class Note < ApplicationRecord attachment :image belongs_to :user with_options presence: true do validates :title validates :body validates :image end end

app/views/notes

new.html.erb

<section class="hero is-success"> <div class="hero-body"> <div class="container"> <h1 class="title"> 新規ノート作成 </h1> </div> </div> </section> <% if @note.errors.any? %> <div class="notification is-danger"> <h2><%= pluralize(@note.errors.count, "error") %> prohibited this object from being saved: not successfully</h2> <ul> <% @note.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-6"> <%= form_for @note do |f| %> <div class="field"> <%= f.label :title, "ノート名", class: "label" %> <%= f.text_field :title, class: "input" %> </div> <div class="field"> <%= f.label :body, "ノート", class: "label" %> <%= f.text_area :body, class: "textarea" %> </div> <div class="field"> <%= f.label :image, "写真", class: "label" %> <%= f.attachment_field :image, class: "input" %> </div> <%= f.submit '投稿', class: "button is-success" %> <% end %> </div> </div> </div> </section>

補足情報(FW/ツールのバージョンなど)

Rails 6.1.4.1
ruby 3.0.2

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問