Im at the home page and I want to make a link to /card/:id
According to rake routes I have to use cart_path.
The carts route looks like this :
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
def index
@carts = Cart.all
end
def show
end
def new
@cart = Cart.new
end
Which uses this file :
module CurrentCart
extend ActiveSupport::Concern
private
def set_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
@cart = Cart.create
session[:cart_id] = @cart.id
end
end
So I have to figure out how to make the id of the card avaible on the url.
Roelof
Op maandag 8 december 2014 14:38:20 UTC+1 schreef Vivek Sampara:
> If you're on "/cards/:id", then look at your cards_controller.rb , you'd
> see @card defined , hence you should use <li><%= link_to '<i class="fa
> fa-money"></i> Card'.html_safe, cart_path(@card) %> </li>
>
> But why would you goto to the show page ( /cards/:id/ ) when you're
> already on the page ? regarding the cart, You have to follow the
> same procedure. define @cart in the controller/action and use it like this
>
> <li><%= link_to '<i class="fa fa-money"></i> Card'.html_safe,
> cart_path(@cart) %> </li>
>
> If you can give more info on what you're trying to do (app structure) , We
> can help you a bit better :)
>
> PS: i haven't read the book
>
> Cheers
> Vivek
>
>
> On Mon, Dec 8, 2014 at 7:00 PM, Roelof Wobben <[email protected]
> <javascript:>> wrote:
>
>> Hello,
>>
>> It's is the form of /cards/:id
>>
>> I tried
>>
>> <li><%= link_to '<i class="fa fa-money"></i> Card'.html_safe,
>> cart_path(cart) %> </li>
>> <li><%= link_to '<i class="fa fa-money"></i> Card'.html_safe,
>> cart_path(card) %> </li>
>>
>> but on both I see this message :
>>
>> undefined local variable or method `cart'
>>
>> undefined local variable or method `card'
>>
>>
>> Roelof
>>
>> Op maandag 8 december 2014 14:23:33 UTC+1 schreef Vivek Sampara:
>>
>>> What is the url you're using to show this on a separate page ?
>>>
>>> if its in the pattern of "/cards/:id/something" then you dont have to
>>> provide the card path as rails picks up @card object to build the path or
>>> you'll have to pass the card variable inside the path method.
>>>
>>> eg
>>>
>>> <% @cards.each do |card| %>
>>> <li><%= link_to '<i class="fa fa-money"></i> Card'.html_safe,
>>> cart_path(card) %> </li>
>>> <% end %>
>>>
>>> This way, rails would know "which" exact card this link should take them
>>> to.
>>>
>>>
>>> On Mon, Dec 8, 2014 at 6:48 PM, Roelof Wobben <[email protected]>
>>> wrote:
>>>
>>>> Hello,
>>>>
>>>> I follow the Agile Web development book.
>>>>
>>>> Now as a extra challenge I try to make the card visible on a seperate
>>>> page.
>>>>
>>>> So I made this link :
>>>>
>>>> <li><%= link_to '<i class="fa fa-money"></i> Card'.html_safe, cart_path
>>>> %> </li>
>>>>
>>>> But now I see the above error message.
>>>> When I do card_path (:id) I see a message that there is no card with
>>>> the id of id.
>>>>
>>>> Roelof
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Ruby on Rails: Talk" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To post to this group, send email to [email protected].
>>>> To view this discussion on the web visit https://groups.google.com/d/
>>>> msgid/rubyonrails-talk/9442f58f-58b5-475e-823d-
>>>> c797431c41c3%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/rubyonrails-talk/9442f58f-58b5-475e-823d-c797431c41c3%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby on Rails: Talk" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/rubyonrails-talk/46cea06c-0a2c-41e2-88c5-b3516c76c504%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/rubyonrails-talk/46cea06c-0a2c-41e2-88c5-b3516c76c504%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/7c0f7fbf-6349-4655-9962-21263fe3951e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.