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

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

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

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

4回答

3828閲覧

Ransack ネストされた子モデルを親モデルごとのshowページで検索したい

besuko

総合スコア16

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2016/07/14 07:45

編集2016/07/15 11:53

##環境
ruby 2.1.0
Rails 4.1.0
ransack 1.7.0

##Ransack ネストされた子モデルを親モデルごとのshowページで検索したい
複数のItem(子モデル)を持つShop(親モデル)があります。
gemRansackshop.show.html.erbにItem一覧を表示し、店舗ごとのitem検索機能を作りたいと考えています。

一覧表示までは出来ていますが、検索とソートが機能しない状況です。
コンソールでは、親モデルだけ指定され全ての結果が返されていますので、検索・ソートが機能していない事までは分かりますが、改善の方法が分からずにおります。

ターミナル Started GET "/shops/1?utf8=%E2%9C%93&q%5Bname_cont%5D=%E5%90%8D%E5%89%8D&commit=%E6%A4%9C%E7%B4%A2" for 10.0.2.2 at 2016-07-15 11:51:37 +0000 Processing by ShopsController#show as HTML Parameters: {"utf8"=>"✓", "q"=>{"name_cont"=>"名前"}, "commit"=>"検索", "id"=>"1"} Shop Load (0.3ms) SELECT `shops`.* FROM `shops` WHERE `shops`.`id` = 1 LIMIT 1 Item Load (0.6ms) SELECT `items`.* FROM `items` WHERE `items`.`shop_id` = 1
routes.rb Rails.application.routes.draw do resources :shops do resources :items end end
models/shop.rb class Shop < ActiveRecord::Base belongs_to :user has_many :items , dependent: :destroy
shops_controller.rb def show @shop = Shop.find(params[:id]) @q = @shop.items.ransack(params[:q]) @q.sorts = 'price asc' if @q.sorts.empty? @shop.items = @q.result(distinct: true) respond_with(@shop.items) end
shop/show.html.erb <h1>店舗詳細</h1> <p> <strong>Image:</strong> <% if @shop.image? %> <td><%= image_tag @shop.image_url(:thumb).to_s , :width => '300'%></td> <% end %> </p> <p> <strong>Name:</strong> <%= @shop.name %> </p> <p> <strong>City:</strong> <%= @shop.city %> <h1>商品一覧</h1> <%= search_form_for @q, url: items_path(params[:shop_id]) do |f| %> <%= f.label :name_or_content_cont, "商品名or説明文" %> <%= f.search_field :name_or_content_cont %> <%= f.submit '検索'%> <% end %> <table> <thead> <tr> <th>ShopName</th> <th>ItemImage</th> <th>ItemName</th> <th><%= sort_link(@q,:item_price,'値段') %></th> <th>Content</th> <th colspan="3"></th> </tr> </thead> <%= render @shop.items %> </table>

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

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

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

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

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

guest

回答4

0

Indexに記述する事でざっくりですが解決しました!

もしかしてRansackはShowでは作動せず、Indexだけで作動するのではと考えて、
Index内で「親(shop)IDがある場合」と「親(shop)IDない場合」で子(item)を分岐させてみました。

items_controller.rb def index #親IDがある場合 if params[:shop_id] @shop = Shop.find(params[:shop_id]) @q = @shop.items.ransack(params[:q]) @q.sorts = 'name asc' if @q.sorts.empty? @items = @q.result(distinct: true) else #無い場合 @q = Item.ransack(params[:q]) @q.sorts = 'name asc' if @q.sorts.empty? @items = @q.result(distinct: true) end respond_with(@items) end
item/index.html.erb <% if @shop %> <h1><%= @shop.name %>:商品検索</h1> <%= search_form_for @q do |f| %> <%= f.label :name, "商品名" %> <%= f.search_field :name_cont %> <%= f.label :price, "値段" %> <%= f.search_field :price_cont %> <%= f.submit '検索'%> <% end %> <% else %> <h1>商品検索</h1> <%= search_form_for @q do |f| %> <%= f.label :name, "商品名" %> <%= f.search_field :name_cont %> <%= f.label :price, "値段" %> <%= f.search_field :price_cont %> <%= f.submit '検索'%> <% end %> <% end %> <table> <thead> <tr> <th>image</th> <th><%= sort_link(@q,:name,'商品名') %></th> <th><%= sort_link(@q,:price,'値段') %></th> <th colspan="3"></th> </tr> </thead> <%= render @items %> </table>

投稿2016/07/16 06:31

besuko

総合スコア16

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

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

0

Indexに記述する事でざっくりですが解決しました!

もしかしてRansackはShowでは作動せず、Indexだけで作動するのではと考えて、
Index内で「親(shop)IDがある場合」と「親(shop)IDない場合」で子(item)を分岐させてみました。

items_controller.rb def index #親IDがある場合 if params[:shop_id] @shop = Shop.find(params[:shop_id]) @q = @shop.items.ransack(params[:q]) @q.sorts = 'name asc' if @q.sorts.empty? @items = @q.result(distinct: true) else #無い場合 @q = Item.ransack(params[:q]) @q.sorts = 'name asc' if @q.sorts.empty? @items = @q.result(distinct: true) end respond_with(@items) end
item/index.html.erb <% if @shop %> <h1><%= @shop.name %>:商品検索</h1> <%= search_form_for @q do |f| %> <%= f.label :name, "商品名" %> <%= f.search_field :name_cont %> <%= f.label :price, "値段" %> <%= f.search_field :price_cont %> <%= f.submit '検索'%> <% end %> <% else %> <h1>商品検索</h1> <%= search_form_for @q do |f| %> <%= f.label :name, "商品名" %> <%= f.search_field :name_cont %> <%= f.label :price, "値段" %> <%= f.search_field :price_cont %> <%= f.submit '検索'%> <% end %> <% end %> <table> <thead> <tr> <th>image</th> <th><%= sort_link(@q,:name,'商品名') %></th> <th><%= sort_link(@q,:price,'値段') %></th> <th colspan="3"></th> </tr> </thead> <%= render @items %> </table>

投稿2016/07/16 06:31

besuko

総合スコア16

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

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

0

Indexに記述する事でざっくりですが解決しました!

もしかしてRansackはShowでは作動せず、Indexだけで作動するのではと考えて、
Index内で「親(shop)IDがある場合」と「親(shop)IDない場合」で子(item)を分岐させてみました。

items_controller.rb def index #親IDがある場合 if params[:shop_id] @shop = Shop.find(params[:shop_id]) @q = @shop.items.ransack(params[:q]) @q.sorts = 'name asc' if @q.sorts.empty? @items = @q.result(distinct: true) else #無い場合 @q = Item.ransack(params[:q]) @q.sorts = 'name asc' if @q.sorts.empty? @items = @q.result(distinct: true) end respond_with(@items) end
item/index.html.erb <% if @shop %> <h1><%= @shop.name %>:商品検索</h1> <%= search_form_for @q do |f| %> <%= f.label :name, "商品名" %> <%= f.search_field :name_cont %> <%= f.label :price, "値段" %> <%= f.search_field :price_cont %> <%= f.submit '検索'%> <% end %> <% else %> <h1>商品検索</h1> <%= search_form_for @q do |f| %> <%= f.label :name, "商品名" %> <%= f.search_field :name_cont %> <%= f.label :price, "値段" %> <%= f.search_field :price_cont %> <%= f.submit '検索'%> <% end %> <% end %> <table> <thead> <tr> <th>image</th> <th><%= sort_link(@q,:name,'商品名') %></th> <th><%= sort_link(@q,:price,'値段') %></th> <th colspan="3"></th> </tr> </thead> <%= render @items %> </table>

投稿2016/07/16 06:30

besuko

総合スコア16

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

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

0

自己解決

Indexに記述する事でざっくりですが解決しました!

もしかしてRansackはShowでは作動せず、Indexだけで作動するのではと考えて、
Index内で「親(shop)IDがある場合」と「親(shop)IDない場合」で子(item)を分岐させてみました。

items_controller.rb def index #親IDがある場合 if params[:shop_id] @shop = Shop.find(params[:shop_id]) @q = @shop.items.ransack(params[:q]) @q.sorts = 'name asc' if @q.sorts.empty? @items = @q.result(distinct: true) else #無い場合 @q = Item.ransack(params[:q]) @q.sorts = 'name asc' if @q.sorts.empty? @items = @q.result(distinct: true) end respond_with(@items) end
item/index.html.erb <% if @shop %> <h1><%= @shop.name %>:商品検索</h1> <%= search_form_for @q do |f| %> <%= f.label :name, "商品名" %> <%= f.search_field :name_cont %> <%= f.label :price, "値段" %> <%= f.search_field :price_cont %> <%= f.submit '検索'%> <% end %> <% else %> <h1>商品検索</h1> <%= search_form_for @q do |f| %> <%= f.label :name, "商品名" %> <%= f.search_field :name_cont %> <%= f.label :price, "値段" %> <%= f.search_field :price_cont %> <%= f.submit '検索'%> <% end %> <% end %> <table> <thead> <tr> <th>image</th> <th><%= sort_link(@q,:name,'商品名') %></th> <th><%= sort_link(@q,:price,'値段') %></th> <th colspan="3"></th> </tr> </thead> <%= render @items %> </table>

投稿2016/07/16 06:30

besuko

総合スコア16

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問