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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

424閲覧

link_toの他のコントロラーのshowアクションへの飛ばし方がわかりません

c.koki

総合スコア14

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/06/08 14:15

line_items/_line_items.html.erbの8行目である
<%= link_to image_tag(line_item.information.image_url(:thumb)) %>によってinformation/show.html.erbに飛ばしたいです

carts/show.html.erbが

ruby

1<div class ="keep-shopping pv1 mt4 has-text-right"> 2 <%= link_to 'Keep looking', information_path, class: 'button is-warning' %> 3</div> 4<hr/> 5 6<section class='section'> 7 <%= render(@cart.line_items) %> 8 9 10 11 <div class="columns"> 12 <div class="column"> 13 <%= button_to 'Empty Cart', @cart , method: :delete, data: {confirm: "Are you sure? " },class: "button is-danger" %> 14 </div> 15 16 <div class="column total has-text-right"> 17 18 </div> 19 </div> 20</section> 21```line_items/_line_items.html.erbが 22```ruby 23<div class="columns align-items-center"> 24 <div class="column is-1"> 25 <%= line_item.quantity %> 26 </div> 27 28 <div class="column is-2"> 29 <figure class="is-128x128 image"> 30 <%#= image_tag(line_item.information.image_url(:thumb)) %> 31 <%= link_to image_tag(line_item.information.image_url(:thumb)) %> 32 </figure> 33 </div> 34 <div class="column is-9"> 35 <strong><%= line_item.information.title %></strong> 36 <div class="columns align-items-center"> 37 <div class="content column is-9"> 38 <%= truncate(line_item.information.description, length: 140) %> 39 </div> 40 41 42 </div> 43 </div> 44</div> 45 46<div class="has-text-right"> 47 <%= link_to 'Remove Item', line_item, method: :delete, data: { confirm:" Are you sure? "}, class: "button is-small mb-4" %> 48</div> 49 50<hr/ >

carts_controllerが

ruby

1class CartsController < ApplicationController 2 rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart 3 before_action :set_cart, only: [:show, :edit, :update, :destroy] 4 5 # GET /carts 6 # GET /carts.json 7 def index 8 @carts = Cart.all 9 end 10 11 # GET /carts/1 12 # GET /carts/1.json 13 def show 14 @information = Information.find(params[:id]) 15 end 16 17 # GET /carts/new 18 def new 19 @cart = Cart.new 20 end 21 22 # GET /carts/1/edit 23 def edit 24 end 25 26 # POST /carts 27 # POST /carts.json 28 def create 29 @cart = Cart.new(cart_params) 30 31 respond_to do |format| 32 if @cart.save 33 format.html { redirect_to @cart, notice: 'Cart was successfully created.' } 34 format.json { render :show, status: :created, location: @cart } 35 else 36 format.html { render :new } 37 format.json { render json: @cart.errors, status: :unprocessable_entity } 38 end 39 end 40 end 41 42 # PATCH/PUT /carts/1 43 # PATCH/PUT /carts/1.json 44 def update 45 respond_to do |format| 46 if @cart.update(cart_params) 47 format.html { redirect_to @cart, notice: 'Cart was successfully updated.' } 48 format.json { render :show, status: :ok, location: @cart } 49 else 50 format.html { render :edit } 51 format.json { render json: @cart.errors, status: :unprocessable_entity } 52 end 53 end 54 end 55 56 # DELETE /carts/1 57 # DELETE /carts/1.json 58 def destroy 59 @cart.destroy if @cart.id == session[:cart_id] 60 session[:cart_id] = nil 61 respond_to do |format| 62 format.html { redirect_to root_path, notice: 'Cart was successfully destroyed.' } 63 format.json { head :no_content } 64 end 65 end 66 67 private 68 # Use callbacks to share common setup or constraints between actions. 69 def set_cart 70 @cart = Cart.find(params[:id]) 71 end 72 73 # Never trust parameters from the scary internet, only allow the white list through. 74 def cart_params 75 params.fetch(:cart, {}) 76 end 77 78 def invalid_cart 79 logger.error "Attempt to access invalid cart #{params[:id]}" 80 redirect_to root_path, notice: "That cart doesn't exist" 81 end 82 83end 84```です 85 86やったこと 87<%= link_to image_tag(line_item.information.image_url(:thumb)),inforamtion_path(@information) %>としたところcartのidが2であったらinformationのidが2のところに飛んでしまいます 88 89おそらくcontrollerで@information = Information.find(params[:id])とありparams[:id]の部分がcart_idと勘違いされているのかと思います。そこで@information = Information.find(params[:information_id])としてもダメでした。 90 91どうしたらinformationのshowに飛ばせるのでしょうか? 92教えてください!

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

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

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

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

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

guest

回答2

0

ベストアンサー

information/show.html.erbへのPathがinformation_pathであれば(rails routes を実行してみて確認してください)、

rails

1<%= link_to information_path(line_item.information), image_tag(line_item.information.image_url(:thumb)) %>

を行うと、information_path ってPath名に与えられたControllerのメソッドが実行されます。
そして、引数がline_item.informationであると、line_itemに紐づいたinformationのIDが渡ります。

投稿2019/06/08 14:50

hatsu

総合スコア1809

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

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

c.koki

2019/06/09 01:19

できました、丁寧に回答ありがとうございます!
guest

0

GET /carts/1

GET /carts/1.json

def show
@information = Information.find(params[:id])
end

まず、このアクションに違和感があります。
route.rb見ないと断定はできませんが、十中八九この場合のparams[:id]はCartのidです。

ヤマカンですが

erb

1<%= link_to image_tag(line_item.information.image_url(:thumb)), 2 inforamtion_path(line_item.information) %>

投稿2019/06/08 14:45

asm

総合スコア15147

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

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

c.koki

2019/06/09 01:19

できました、丁寧に回答ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問