前提・実現したいこと
現在、railsにて顧客管理のアプリを作成しているのですが、
id 1のページのみ非表示にしたいのですが、うまく行きません
何卒、よろしくお願いいたします
該当のソースコード
searches/index.html.erb
thml
1<table> 2 <thead class = "category"> 3 <tr> 4 <th>名前</th> 5 <th>フリカナ</th> 6 <th>性別</th> 7 <th>年齢</th> 8 <th>会社名</th> 9 <th>役職</th> 10 <th>郵便番号</th> 11 <th>住所</th> 12 <th>電話番号</th> 13 <th colspan="9"></th> 14 </tr> 15 </thead> 16 <tbody> 17 <% @searches.each do|search| %> 18 19 <tr> 20 <th class = addless><%= search.family_name %> 21 <%= search.first_name %>  </th> 22 <th class = addless><%= search.family_name_kana %> 23 <%= search.first_name_kana %>  </th> 24 <th class = addless><%= search.sex %>  </th> 25 <th class = addless><%= search.age %>  </th> 26 <th class = addless><%= search.company %>  </th> 27 <th class = addless><%= search.position %></th> 28 <% search.postal_code.to_s.insert(3, "-")%> 29 <th class = addless><%= search.postal_code %>  </th> 30 <th class = addless><%= search.address1 %> 31 <%= search.address2 %> 32 <%= search.address3 %> 33 <%= search.building_name %>  </th> 34 <th class = addless><%= search.phone_number %></th> 35 <th> <%= link_to '詳細', search,class: "button is-info" %> </th> 36 <th><%= link_to '編集', edit_search_path(search.id),class: "button is-success" %> </th> 37 <th><%= link_to '削除',search_path(search.id), method: :delete,class: "button is-danger", data: { confirm: '本当に削除してよろしでしょうか?' } %></th> 38 </tr> 39 <% end %> 40 </tbody> 41</table>
searches_controller.rb
ruby
1class SearchesController < ApplicationController 2 def show 3 @search = Search.find(params[:id]) 4 end 5 6 def index 7 @searches = Search.all 8 end 9 10 def edit 11 @search = Search.find(params[:id]) 12 end 13 14 def update 15 @search = Search.find(params[:id]) 16 @search.update(update_params) 17 redirect_to root_path 18 end 19 20 21 def create 22 @search = Search.new(update_params) 23 @search.user_id = current_user.id 24 @search.save 25 redirect_to searches_path 26 end 27 28 def destroy 29 search = Search.find(params[:id]) 30 search.destroy 31 redirect_to root_path 32 end 33 34 def search 35 @search = Search.new 36 if postal_code = params[:postal_code] 37 params = URI.encode_www_form({zipcode: postal_code}) 38 uri = URI.parse("http://zipcloud.ibsnet.co.jp/api/search?#{params}") 39 response = Net::HTTP.get_response(uri) 40 result = JSON.parse(response.body) 41 if result["results"] 42 @zipcode = result["results"][0]["zipcode"] 43 @address1 = result["results"][0]["address1"] 44 @address2 = result["results"][0]["address2"] 45 @address3 = result["results"][0]["address3"] 46 end 47 end 48 end 49end 50 51private 52 def search_params 53 params.permit(:address1, :address2, :address3, :building_name,:phone_number, :postal_code,:family_name,:first_name,:family_name_kana,:family_name_kana,:first_name_kana,:sex,:age,:company,:position ) 54 end 55 56 def update_params 57 params.require(:search).permit(:address1, :address2, :address3, :building_name,:phone_number, :postal_code,:family_name,:first_name,:family_name_kana,:family_name_kana,:first_name_kana,:sex,:age,:company,:position) 58 end
routes.rb
ruby
1Rails.application.routes.draw do 2 get 'searches',to: "searches#index" 3 mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 4 devise_for :users, controllers: { 5 omniauth_callbacks: 'users/omniauth_callbacks', 6 registrations: 'users/registrations' 7 } 8 post '/callback' => 'linebot#callback' 9 get 'homes/index' 10 resources :homes, only: [:index] 11 root to: 'home#index' 12 resources :users 13 resources :attendances 14 resources :daytimes 15 resources :restaurants, only: [:index, :show] 16 resources :searches do 17 collection do 18 get 'search' 19 end 20 end 21 namespace :admin do 22 resources :restaurants, only: [:index, :new, :create, :show, :edit, :destroy] 23 end 24end 25
試したこと
index.html.erbの <% @searches.each do|search| %>の後に<% unless @searches_id == 1 %>を記述しましたが、うまくidを取り出せなくエラーも起こりません
<% unless searches_id == 1 %>だとNameError
undefined local variable or method `searches_id' for #<#Class:0x00007fedd2e44dd8:0x00007fedd2e4e6d0>
Did you mean? searches_url
@searches
と表記されます。
補足情報(FW/ツールのバージョンなど)
ruby '2.6.5'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/29 09:04
2020/10/29 10:07
2020/10/30 03:05
2020/10/30 03:31
2020/10/30 03:44