80494


ソースコード

ルーティング

[code]

    get "search", to: "posts#search"
    
[/code]

検索フォーム

[code]

    <%= search_form_for @q, url: search_path do |f| %>
    <%= f.search_field :shopaddress_cont, class: 'form-control', placeholder: "住所を入れてください" %>
    <%= f.submit'検索',class: 'btn btn-success' %>
    <% end %>
    
[/code]

コントローラー

[code]

    before_action :set_q, only: [:index, :search]
    
        def index
          @posts = Post.all
          @post = Post.new
          @kaminari= Post.all.page(params[:page]).per(6)
        end
        
        def search
          @results = @q.result
        end
    
    private
    
        def set_q
          @q = Post.ransack(params[:q])
        end
    
[/code]

[code]

    # Strong Parameter
    def post_params
      params.require(:post).permit(:shopname, :shopaddress, 
                                   :shopcontent)
     end
    
[/code]

表示させたいview

search.html.erb

[code]

    <tbody>
    <% @results.each do |searchresult| %>
    <div class = "searchresultbox"></div>
    <br><%= searchresult.shopname %></br>
    <br><%= searchresult.shopaddress %></br>
    <br><%= searchresult.shopcontent %></br>
    </div>
    
    <% end %>
    </tbody>
    
[/code]

初期画面全体view(検索フォーム含む）

[code]

    <div class="navbar-brand"><%= link_to 'ピックアップしに行ったらこんなところで 
      した', root_path %><i class="fab fa-uber"></i></div>
    
    
    <form class="form-inline">
      近くのお店を調べてみる
      <%= search_form_for @q, url: search_path do |f| %>
        <%= f.search_field :shopaddress_cont, class: 'form-control', 
        placeholder: "住所を入れてください" %>
        <%= f.submit'検索',class: 'btn btn-success' %>
      <% end %>
    </form>
    </nav>
    <div class="container">
      <div class="row">
       <div class="mx-auto" style="width: 50rem;" >
        <div class="list-group list-group-flush">
         <div class="col-md-6">
           <% @posts.each do |post| %>
             <div class="list-group-item list-group-item-success mt-5 
               border-left border-right"><%= post.shopname %></div>
             <div class="list-group-item border-left border-right"> 
               <%= post.shopaddress %></div>
             <div class="list-group-item border-left border-right 
               border-bottom" style="height: 8rem;"><%= 
               post.shopcontent %></div>
           <% end %>
          </div>
         </div>
        </div>
       </div>
      </div>
    
[/code]

検索ボタン押した際のログ  
Started GET "/" for 103.5.140.166 at 2021-07-29 23:44:32 +0000  
Cannot render console from 103.5.140.166! Allowed networks: 127.0.0.1, ::1,
127.0.0.0/127.255.255.255  
Processing by PostsController#index as HTML  
Rendering posts/index.html.erb within layouts/application  
Post Load (0.3ms) SELECT `posts`.* FROM `posts`  
↳ app/views/posts/index.html.erb:19  
(0.2ms) SELECT COUNT(*) FROM `posts`  
↳ app/views/posts/index.html.erb:32  
Rendered posts/index.html.erb within layouts/application (20.8ms)  
Completed 200 OK in 25ms (Views: 22.7ms | ActiveRecord: 0.6ms)


CC BY-SA 4.0
2021-07-29T02:19:48.407
80450
2021-07-31T02:32:02.613
2021-07-30T23:08:29.513
47497
47497
question
ruby-on-rails ruby rubygems
ransackを使用してsearch.html.erb(検索結果）を表示させたいです　検索ボタンを押すと初期画面のindex.html.erbが表示されてしまいます


`search_form_for` のフォームヘルパーがformタグを出力するので外側で囲うタグを削除して試してみてください。

[code]

    <%= search_form_for @q, url: search_path, class: "form-inline" do |f| %>
      <%= f.label :title, "近くのお店を調べてみる" %>
      <%= f.search_field :title, class: "form-control", placeholder: "please name" %>
      <%= f.submit "検索", class: "btn btn-success" %>
    <% end %>
    
[/code]


CC BY-SA 4.0
2021-07-31T02:32:02.613
80494
2021-07-31T02:32:02.613
298
80450
answer