Reputation: 269
Following a RoR turorial where I am building a basic bookmark app.
I have two models, topic
and bookmark
.
Bookmarks have a url
attribute. The url
attribute is rendering fine on my topics#show
, but as plain text. When I tried to render it as a link it doesn't link to the url properly.
How do I render it as a hyperlink?
I have tried this
<%= @topic.bookmarks.each do |bookmark| %>
<a href="#{bookmark.url}">bookmark.url</a>
<% end %>
But clearly that doesn't look right. Am I interpolating the correct way?
Alternatively is there a rails helper method that would do the trick?
Here are my files
Topics controller
class TopicsController < ApplicationController
def index
@topics = Topic.all
end
def new
@topic = Topic.new
end
def show
@topic = Topic.find(params[:id])
end
def create
@topic = Topic.new(params.require(:topic).permit(:name))
if @topic.save
redirect_to @topic
else
render :new
end
end
end
My bookmarks controller
class BookmarksController < ApplicationController
def create
@topic = Topic.find(params[:topic_id])
@bookmarks = @topic.bookmarks
@bookmark = @topic.bookmarks.build(params.require(:bookmark).permit(:url, :topic_id))
@bookmark.topic = @topic
@new_bookmark = Bookmark.new
if @bookmark.save
flash[:notice] = "Bookmark was saved"
redirect_to @topic
else
flash[:error] = "There was an error, please try again later"
redirect_to @topic
end
end
def destroy
@topic = Topic.find(params[:topic_id])
@bookmark = Bookmark.find(params[:id])
@bookmark.topic = @topic
if @bookmark.destroy
flash[:notice] = "Bookmark was destroyed successfully"
redirect_to [@topic]
else
flash[:error] = "There was an error, please try again later"
end
end
end
And these are my migration files
class CreateTopics < ActiveRecord::Migration
def change
create_table :topics do |t|
t.string :name
t.timestamps
end
end
end
class CreateBookmarks < ActiveRecord::Migration
def change
create_table :bookmarks do |t|
t.string :url
t.references :topic, index: true
t.timestamps
end
end
end
And here is my routes file
Rails.application.routes.draw do
resources :topics do
resources :bookmarks, only: [:destroy, :create]
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
Bookmark form partial which is displayed in topics#show
<%= form_for [@topic, @topic.bookmarks.new] do |f| %>
<div class="col-md-5">
<div class="form-group">
<%= f.text_field :url, placeholder: "Enter bookmark url", class: 'form-control' %>
</div>
<%= f.submit "save", class: 'form-control' %>
</div>
<% end %>
in topics#show
added this line to render partial
<%= render partial: 'bookmarks/form', locals: { topic: @topic, bookmark: @bookmark} %>
Upvotes: 0
Views: 589
Reputation: 529
You can use the link_to
helper method:
<%= @topic.bookmarks.each do |bookmark| %>
<%= link_to bookmark.url, bookmark.url %>
<% end %>
More info here
Upvotes: 1
Reputation: 23799
ERB does not interpolate strings unless they are inside ERB blocks (<%
... %>
).
I.e., in your case the following would work:
<a href="<%= bookmark.url %>">bookmark.url</a>
The cleaner solution is to use link_to
as was mentioned in another answer. I just thought it's important to understand why the original solution did not work.
Upvotes: 1
Reputation: 572
Have you tried to use the helper link_to
?
<%= link_to 'name', bookmark.url, class: 'btn btn-default' %>
Upvotes: 3