cat
cat

Reputation: 749

How can I make link_to in this case?

I want the link which goes to

http://example.com/shop/:shop_name

so it should be something like this.

<%= link_to "Shop", req.host + "/shop/"+ @shop.shop_name , :class => 'btn' %>

I don't want to use something_path or something_url here.

I just want to create url link from current host, and variable.
How can I?

UPDATE:

  <%= link_to "Shop", request.host + /shop/ [email protected]_name , :class => 'btn' %>

This takes me to

http://www.example.com/shop/www.example.com/shop/walmart

Upvotes: 0

Views: 55

Answers (2)

Jiř&#237; Posp&#237;šil
Jiř&#237; Posp&#237;šil

Reputation: 14402

Why don't you want to use url helpers? Doing it by hand is error-prone.

Simply put this to routes.rb

get "shop/:name", to: "shops#show", as: "shop_name"

Then you can use this in your templates:

<%= link_to "Shop", shop_name_path(@shop.shop_name), :class => 'btn' %>

In the show action of shops controller just fetch the name param:

shop_name = params[:name]

Upvotes: 1

shweta
shweta

Reputation: 8169

try with,

<%= link_to "Shop", "/shop/"+ @shop.shop_name , :class => 'btn' %>

Upvotes: 1

Related Questions