@inrmnn

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Rails ransackでのエラー

解決したいこと

webサイトにてransackの使い方を検索し実装したのですがエラーが出てしまいうまく作動しません

発生している問題・エラー

RuntimeError in Public::Posts#index
Showing /home/ec2-user/environment/reco_ani/app/views/public/posts/index.html.erb where line #6 raised:

Ransack needs Post attributes explicitly allowlisted as
searchable. Define a `ransackable_attributes` class method in your `Post`
model, watching out for items you DON'T want searchable (for
example, `encrypted_password`, `password_reset_token`, `owner` or
other sensitive information). You can use the following as a base:

```ruby
class Post < ApplicationRecord

  # ...

  def self.ransackable_attributes(auth_object = nil)
    ["body", "created_at", "genre_id", "id", "title", "updated_at", "user_id"]
  end

  # ...

end

posts_controller

class Public::PostsController < ApplicationController
  before_action :authenticate_user!
  before_action :search
  def search
  @q = Post.ransack(params[:q])
   
  end
  
  def new
    @post = Post.new
    @genres = Genre.all
  end
  
  def create
    @post = Post.new(post_params)
    @post.user_id = current_user.id
    @post.save!
    redirect_to posts_path
  end
  
  def index
    @posts = @q.result(distinct: true)
    @posts = Post.page(params[:page])
    @user = current_user
    @genres = Genre.all
       if params[:genre_id]
         @genre = Genre.find(params[:genre_id])
         @posts = @genre.posts.page(params[:page])
       else
         @posts = Post.page(params[:page])
       end 
  end

  def show
    @post = Post.find(params[:id])
    @user = @post.user
    @comment = Comment.new
    
  end
  
  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to user_path(current_user)
  end 
  
  def edit
    @post = Post.find(params[:id])
    @genres = Genre.all
  end 
  
  def update
    @post = Post.find(params[:id])
    @post.user_id = current_user.id
    @post.update(post_params)
    redirect_to user_path(current_user)
  end
  
  def comment
     @post = Post.find(params[:id])
     @comment = @post.comments.page(params[:page])
  end
  private
  
  def post_params
     params.require(:post).permit(:title, :body, :genre_id, :comm)
  end
end

routing

Rails.application.routes.draw do
 
  devise_for :users, controllers: {
      registrations: "public/registrations",
      sessions: 'public/sessions'
    }
  devise_for :admin, skip: [:registrations, :passwords] ,controllers: {
      sessions: "admin/sessions"
  }
  
  namespace :admin do
    resources :users, only: [:index, :show, :destroy]
   
    resources :genres, only: [:index, :create, :edit, :update, :destroy]
   
    resources :posts, only: [:show, :destroy]
    root to: 'posts#index'
    get 'posts/comments/:id' => 'posts#comments' , as: 'comments'
    delete 'posts/destroy_comment/:id' => 'posts#destroy_comment' , as: 'destroy_comment'
  
   
  end
  
  scope module: :public do
    resources :posts, only: [:new, :create, :index, :show, :edit, :update, :destroy] do
       collection do
        get 'search'
       end
       resources :comments, only: [:create]
     end 
    get 'posts/comment/:id' => 'posts#comment' , as: 'comment'
    
    resources :users, only: [:show, :edit, :update]
    
    get 'homes/about'
    
    root to: 'homes#top'
  end
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end


index.html.erb

<div class="container">
    <div class="row">
        <h1>タイトル検索</h1>
        <%= search_form_for @q, url: search_posts_path do |f| %>
          <%= f.label :title_cont, 'タイトル名' %>
          <%= f.search_field :title_cont %>
          <br>
          <%= f.submit '検索' %>
        <% end %>
    </div>
    <div class="row">
        <div class="col-md-3 mt-5">
            <div class="border border-primary rounded">
                
                <h3 class="bg-primary text-white text-center py-3 mb-3">ジャンル検索</h3>
                 <table class="table table-borderless genre_table">
                       <tbody>
                          <% @genres.each do |genre| %>
                          <tr>
                              <th class=" text-center h4">
                                <%= link_to posts_path(genre_id: genre.id), class:"text-dark text-border text-decoration-none" do %>
                                
                                 <%= genre.name %>
                                <% end %>
                              </th>
                           </tr> 
                          <% end %>
                    </tbody>
                  </table>
            </div>    
        </div>
        <div class="col-md-7 offset-md-1 mt-5">
            <div class="border border-primary rounded">
                <h3 class="bg-primary text-white text-center py-3 mb-0">
                    投稿一覧
                </h3>
            
    
                <div class="row">
                    <div class="col-12">
                        <table class="table table-borderless">
                                <thead class="table-secondary">
                                       <tr>
                                           <th>アニメ名</th>
                                           <th>投稿者</th>
                                           <th>ジャンル</th>
                                           <th>投稿日時</th>
                                       </tr>
                                </thead>
                                <tbody>
                                        <% @posts.each do |post| %>
                                                <tr>
                                                    <td>
                                                        <%= link_to post_path(post) do %>
                                                          <%= post.title %>
                                                        <% end %>
                                                    </td>
                                                    <td>
                                                        <%= link_to user_path(post.user) ,class: "text-dark" do %>  
                                                           <%= post.user.name %>
                                                        <% end %>
                                                    </td>
                                                    <td>
                                                        <%= post.genre.name %>
                                                    </td>
                                                    <td>
                                                        <%= post.updated_at.strftime('%Y/%m/%d %H:%M') %>
                                                    </td>
                                                </tr>
                                        <% end %>
                                </tbody>
                        </table>
                         <%= paginate(@posts) %>
                    </div>
                </div>
            </div>    
        </div>   
    </div>    
</div>

search.html.erb

<div class="row">
    <div class="col-md-7 offset-md-1 mt-5">
            <div class="border border-primary rounded">
                <h3 class="bg-primary text-white text-center py-3 mb-0">
                    投稿一覧
                </h3>
            
    
                <div class="row">
                    <div class="col-12">
                        <table class="table table-borderless">
                                <thead class="table-secondary">
                                       <tr>
                                           <th>アニメ名</th>
                                           <th>投稿者</th>
                                           <th>ジャンル</th>
                                           <th>投稿日時</th>
                                       </tr>
                                </thead>
                                <tbody>
                                        <% @results.each do |post| %>
                                                <tr>
                                                    <td>
                                                        <%= link_to post_path(post) do %>
                                                          <%= post.title %>
                                                        <% end %>
                                                    </td>
                                                    <td>
                                                        <%= link_to user_path(post.user) ,class: "text-dark" do %>  
                                                           <%= post.user.name %>
                                                        <% end %>
                                                    </td>
                                                    <td>
                                                        <%= post.genre.name %>
                                                    </td>
                                                    <td>
                                                        <%= post.updated_at.strftime('%Y/%m/%d %H:%M') %>
                                                    </td>
                                                </tr>
                                        <% end %>
                                </tbody>
                        </table>
                         <%= paginate(@posts) %>
                    </div>
                </div>
            </div>    
        </div>   
</div>

自分で試したこと

controllerの書き方がサイトによって違かったので一通り試してみましたがうまくいきませんでした

0 likes

1Answer

Your answer might help someone💌