コントローラにて、.createでレコードを生成し、その際にSecureRandomでIDも発行しているのですが、その値をビューのリンク先に渡したいが、渡し方がわからない。
URL直接入力にて、ルーティングは問題なく、/rounds/play/:round_idを入力してやれば表示はできております。
情報不足かもしれませんが、宜しくお願い致します。
rb
1#search.html.erb 2<% @json["Items"].each do|item| %> 3 <%= link_to item["Item"]["golfCourseName"] , round_play_path(round_id: "ここがわからない") %> <br> 4<% end %>
rb
1#/rounds_controller.rb 2 class RoundsController < ApplicationController 3 require 'securerandom' 4 5 def index 6 7 end 8 def select_course 9 10 @todoufukens = { 11 0 => "全地域", 12 101 => "北海道・東北のすべて", 13 1 => "北海道", 14 2 => "青森県", 15 3 => "岩手県", 16 4 => "宮城県", 17 5 => "秋田県", 18 6 => "山形県", 19 7 => "福島県", 20 102 => "関東のすべて", 21 8 => "茨城県", 22 9 => "栃木県", 23 10 => "群馬県", 24 11 => "埼玉県", 25 12 => "千葉県", 26 13 => "東京都", 27 14 => "神奈川県", 28 19 => "山梨県", 29 20 => "長野県", 30 22 => "静岡県", 31 103 => "北陸のすべて", 32 15 => "新潟県", 33 16 => "富山県", 34 17 => "石川県", 35 18 => "福井県", 36 104 => "中部のすべて", 37 21 => "岐阜県", 38 23 => "愛知県", 39 24 => "三重県", 40 105 => "近畿のすべて", 41 25 => "滋賀県", 42 26 => "京都府", 43 27 => "大阪府", 44 28 => "兵庫県", 45 29 => "奈良県", 46 30 => "和歌山県", 47 106 => "中国のすべて", 48 31 => "鳥取県", 49 32 => "島根県", 50 33 => "岡山県", 51 34 => "広島県", 52 35 => "山口県", 53 107 => "四国のすべて", 54 36 => "徳島県", 55 37 => "香川県", 56 38 => "愛媛県", 57 39 => "高知県", 58 108 => "九州・沖縄のすべて", 59 40 => "福岡県", 60 41 => "佐賀県", 61 42 => "長崎県", 62 43 => "熊本県", 63 44 => "大分県", 64 45 => "宮崎県", 65 46 => "鹿児島県", 66 47 => "沖縄県", 67 109 => "海外" 68 } 69 end 70 71 def search 72 require 'net/http' 73 require 'json' 74 require 'uri' 75 76 @todoufukens = { 77 0 => "全地域", 78 101 => "北海道・東北のすべて", 79 1 => "北海道", 80 2 => "青森県", 81 3 => "岩手県", 82 4 => "宮城県", 83 5 => "秋田県", 84 6 => "山形県", 85 7 => "福島県", 86 102 => "関東のすべて", 87 8 => "茨城県", 88 9 => "栃木県", 89 10 => "群馬県", 90 11 => "埼玉県", 91 12 => "千葉県", 92 13 => "東京都", 93 14 => "神奈川県", 94 19 => "山梨県", 95 20 => "長野県", 96 22 => "静岡県", 97 103 => "北陸のすべて", 98 15 => "新潟県", 99 16 => "富山県", 100 17 => "石川県", 101 18 => "福井県", 102 104 => "中部のすべて", 103 21 => "岐阜県", 104 23 => "愛知県", 105 24 => "三重県", 106 105 => "近畿のすべて", 107 25 => "滋賀県", 108 26 => "京都府", 109 27 => "大阪府", 110 28 => "兵庫県", 111 29 => "奈良県", 112 30 => "和歌山県", 113 106 => "中国のすべて", 114 31 => "鳥取県", 115 32 => "島根県", 116 33 => "岡山県", 117 34 => "広島県", 118 35 => "山口県", 119 107 => "四国のすべて", 120 36 => "徳島県", 121 37 => "香川県", 122 38 => "愛媛県", 123 39 => "高知県", 124 108 => "九州・沖縄のすべて", 125 40 => "福岡県", 126 41 => "佐賀県", 127 42 => "長崎県", 128 43 => "熊本県", 129 44 => "大分県", 130 45 => "宮崎県", 131 46 => "鹿児島県", 132 47 => "沖縄県", 133 109 => "海外" 134 } 135 base_url = "https://app.rakuten.co.jp/services/api/Gora/GoraGolfCourseSearch/20170623" 136 137 k = params[:keyword] 138 t = params[:todoufuken] 139 140 params = { 141 'keyword' => URI.encode(k), 142 'areaCode' => t, 143 'format' => 'json', 144 'applicationId' => '1083401508369226218', 145 'hits' => 30 146 } 147 148 uri = URI(base_url + '?' + params.map{|k,v| "#{k}=#{v}"}.join('&')) 149 puts uri 150 151 # begin 152 response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| 153 http.open_timeout = 5 154 http.read_timeout = 10 155 http.get(uri.request_uri) 156 end 157 158 # case response 159 # when Net::HTTPSuccess 160 @json = JSON.parse(response.body) 161 # end 162 163 # rescue => e 164 # Rails.logger.error(e.message) 165 # raise e 166 # end 167 end 168 def show # 1〜18H分ゴルフ場の名前をpostし、レコードを生成する処理(hole_scoreはNull) 169 @coursename = params[:golfCourseName] 170 @round_id = Message.where(course: params[:course]) 171 round_id = SecureRandom.hex(8) 172 # hoge = params[:round_id] 173 [*1..18].each do|num| 174 Score.create!(course: @coursename,hole_number: num,user_id: 1,round_id: round_id,hole_score: 0) 175 176 # redirect_to home_index_path 177 178 end 179 end 180 def play 181 require "date" 182 @day = Date.today 183 184 @coursename = params[:round_id] 185 @score_card_score = Score.where(round_id: params[:round_id]) #ホールごとのスコアを取得 186 @score_card_course = Score.where(round_id: params[:round_id]).first #ラウンドしたコース、日付を取得 187 @score_sum = Score.where(round_id: params[:round_id]).sum(:hole_score) 188 189 #ランキング関係 190 @rank_user = Score.distinct.pluck(:course, :user_id, :round_id) 191 @rank_today = Score.where(course: params[:course]).where("created_at >= ?", Date.today).group(:round_id).count 192 @rank_today2 = Score.group(:round_id).count 193 # @rank_today3 = Score.find_by(course: params[:course], created_at >= ?", Time.zone.now.beginning_of_day) 194 end 195end 196
rb
1#routes.rb 2 post "/rounds/search", to: 'rounds#search' 3 get '/rounds/search/:golfCourseName', to: 'rounds#show' ,as: "round_start" 4 get '/rounds/play/:round_id', to: 'rounds#play' ,as: "round_play" 5
回答2件
あなたの回答
tips
プレビュー