実現したいこと
カテゴリーの一覧からリンク化したカテゴリーを踏むと、踏んだカテゴリーに該当する投稿の一覧を表示させたいです
発生している問題・エラーメッセージ
表示しているリンクを踏むと以下のエラーがでます
wrong number of arguments (given 2, expected 0..1)
該当のソースコード
関連ファイル
erb
1 2class Book < ApplicationRecord 3 extend ActiveHash::Associations::ActiveRecordExtensions 4 acts_as_taggable 5 belongs_to_active_hash :category 6 belongs_to_active_hash :status 7 belongs_to :user 8 has_one_attached :image 9 with_options presence: true do 10 validates :name 11 validates :price, numericality: { greater_than_or_equal_to: 150, less_than_or_equal_to: 9_999_999 } 12 validates :category_id 13 validates :status_id 14 validates :user 15 validates :author 16 end 17 validates :category_id, :status_id, numericality: { other_than: 1 } 18 def self.category(category) 19 if 1 < category.to_i && category.to_i < 19 20 Category.where('category_id LIKE(?)',"%#{category}%") 21 else 22 Category.all 23 end 24 end 25end
エラー画面で表示されるコード
erb
1 def self.category(category) 2 if 1 < category.to_i && category.to_i < 19 3 Category.where('category_id LIKE(?)',"%#{category}%") 4 else 5 Category.all 6 end 7 end
一応関連するかもと思ったので載せておきます
class BooksController < ApplicationController def index @books = Book.order('created_at DESC') if params[:tag] @books = Book.tagged_with(params[:tag]) else @books = Book.all end @categorys = Category.where.not(id: 1) end def new @book = Book.new @tags = ActsAsTaggableOn::Tag.all end def category @books = Book.category(params[:id]) end def create @book = Book.new(book_params) if @book.save redirect_to root_path else render :new end end private def book_params params.require(:book).permit(:name, :price, :category_id, :status_id, :tag_list, :text, :author, :image).merge(user_id: current_user.id) end end
Rails.application.routes.draw do devise_for :users resources :books do member do get 'category' end end root to: 'books#index' get 'tags/:tag', to: 'books#index', as: :tag end
試したこと
質問できる人もおらず知識もないので解決案が見つけられず力を貸していただけたらと思いました
補足情報(FW/ツールのバージョンなど)
ruby 2.6.5
rails 6.0.0
エラーになるページのViewのコードはありますか?載せていただけると参考になるかもです
category.html.erb
<%= @books.each do |book| %>
<%= book.name %>
<% end %>
まだ内容は詳しく書いていないのでこれだけですがお願いします
<%= @books.each do |book| %> ここは=がいらないので <% @books.each do |book| %>ですね。
wrong number of arguments (given 2, expected 0..1)のエラーが出ている場所、ログを見るとどのファイルの何行目とか書かれているかと思うのですがわかりますでしょうか。
エラー内容としては、引数が0か1個であるべきなところに2個渡しているのが原因です。質問内では該当の箇所がまだ見つけられず。
app/models/book.rb:20:in `category'
18 def self.category(category)
19 if 1 < category.to_i && category.to_i < 19
20 Category.where('category_id LIKE(?)',"%#{category}%")
21 else
22 Category.all
23 end
24 end
app/controllers/books_controller.rb:44:in `category'
43 def category
44 @books = Book.category(params[:id])
45 end
ログではここでwrong number of arguments (given 2, expected 0..1)と出ています。
よろしくお願いします
belongs_to_active_hashのcategoryとdef self.category(category)でcategoryの名前が衝突しているのが気になりますね。
Book.category(params[:id])でやりたいことはと以下のようなことだったりしますか
if 1 < params[:id].to_i && params[:id].to_i < 19
Book.where(category_id: params[:id])
else
Book.all
end
if 1 < category.to_i && category.to_i < 19
Book.where(category_id: category)
else
Book.all
end
で書き換えたらうまくいきました!ありがとうございます!
回答1件
あなたの回答
tips
プレビュー