Forum: Ruby on Rails Embedding link_to's within text fetched from database.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sam D. (Guest)
on 2006-06-04 13:47
(Received via mailing list)
Hi,

I have the following problem that I can't seem to figure out.  I want to
able to fetch some text from my database record in my controller, and
then
add two links within the text and then send that back to the view to get
rendered so that the two links appear as links and when clicked on,
they'd
basically call the corresponding action for the link.  Would anybody
know
how to go about doing this?  How do you get a view to render the link
out?

Thanks and any help appreciated.
mh789 (Guest)
on 2006-06-04 14:44
In your controller, you just take the @text from the database, copy it
to a new member variable @text_with_links, add a new link "by hand" to
that variable (@text_with_links += '<a href="' + url_for(action =>
'...') + '">link</a>") and last render @text_with_links instead of @text
at your view.

If the link was not to an action, you could use auto_link in your view.
<%= auto_link("Visit http://example.com") %> renders as
Visist <a href="http://example.com">http://example.com</a>
Kevin O. (Guest)
on 2006-06-04 16:13
(Received via mailing list)
On Sunday, June 04, 2006, at 12:44 PM, mh789 wrote:
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>removed_email_address@domain.invalid
>http://lists.rubyonrails.org/mailman/listinfo/rails

You could also use an inline template

render :inline=>"<%= @text_from_database %> <%= link_to 'link',
:action=>'action' %>"'

_Kevin
Guest (Guest)
on 2006-06-04 16:22
Kevin O. wrote:
> On Sunday, June 04, 2006, at 12:44 PM, mh789 wrote:
>>
>>--
>>Posted via http://www.ruby-forum.com/.
>>_______________________________________________
>>Rails mailing list
>>removed_email_address@domain.invalid
>>http://lists.rubyonrails.org/mailman/listinfo/rails
>
> You could also use an inline template
>
> render :inline=>"<%= @text_from_database %> <%= link_to 'link',
> :action=>'action' %>"'
>
> _Kevin

Is there any way to do this outside of a controller?
Kevin O. (Guest)
on 2006-06-04 16:37
(Received via mailing list)
On Sunday, June 04, 2006, at 2:22 PM, Guest wrote:
>> You could also use an inline template
>_______________________________________________
>Rails mailing list
>removed_email_address@domain.invalid
>http://lists.rubyonrails.org/mailman/listinfo/rails

Doing this in a view would be trivial, doing it in a model would
probably be a bad idea.
What exactly did you have in mind?

_Kevin
Sam D. (Guest)
on 2006-06-05 12:07
(Received via mailing list)
The problem is, if adding the links is dynamically determined, then in
your
view, you'd need some way to know that some particular database fetched
text
needs links while others don't.  If you do it in the controller, then
your
logic just goes there and you just embed the links when need be.

thanks.


On 4 Jun 2006 12:34:26 -0000, Kevin O. <
This topic is locked and can not be replied to.