how to save links in a database and display them - ruby-on-rails

i am working on a rails blog app, and i need to add user tags on post.
example i write and post the following; 'my dogs name is john. #dogs'
i want dogs to be clickable text on that post.
how do i display a clickable #dogs text on view to a route /dogs.
on a regular views page we just use a link_to helper,
how can i use that or anything else to do so

You could write a view helper and pass the text into that view helper. The View helper can parse the text using regular expressions and replace the results with a link.

Related

Display body content as title when Title not present

I am currently working on a web application using Ruby on Rails and wanted to know how I would go about displaying the body contents as the title dynamically if there is no title present? I have validations to only allow titles to be up to 20 characters so I would like to only have it display the first 20 characters of the body as the title should not be present. I'm mostly trying to understand how I would accomplish this would it be by implementing a partial view or utilizing a or statement between how I would be passing in the title?
A very simple approach would be:
<%= instance.title.presence || truncate(instance.body, length: 20) %>
Note that you have to replace instance with your variable name. When this is used in more than one place then you might want to consider a different approach, like a helper method or a method defined on the model.
Read about Object#presence and the helper method truncate.

How to edit a model instance's text field (storing HTML) using markup/down editor?

(app is built on Rails 4.0.3/postgres)
I have a model defined where one of the attributes is a text field containing the entire HTML of a webpage- I store it as text, which I then set as an instance variable (#html) in the controller and then render it through a view using <%=raw #html %>. This allows me to store and render entire pages easily.
My question is, I need to allow users to edit the HTML in-browser using some kind of markup language/editor, so how would I go about doing so? The workflow would be that the user clicks on an instance of the model through a dashboard, and then is able to edit the name of model instance (easy), and under that is able to edit the html attribute and save it via some kind of markup editor like Github's gist editor. I feel like this should be easy but can't figure it out- can anyone point me in the right direction?
Thanks!

Rails: Filter content using link_to (links) instead of checkbox

I want to filter my content based on tags. I would rather use links instead of a checkbox form that filters content based on which tags are checked by the user. I currently generate links using the code below:
link_to tag.name, user_path(tags: tag.id)
This creates urls of the format ?tag=15
This works to display all content with tag=15 but the user should be able to select multiple tags.
When a user clicks on a second filter link the new tag should be appended to the tag param and now content with both tags should be displayed. Basically, I would like to generate urls that look like:
?tag[]=15&tag[]=1
Is it even possible to specify the tags param as an array in link_to? If so, how can I append subsequent tags to the end of the url? Is there a better way to accomplish filtering through links?
I would create the links like this:
link_to tag.name, user_path(tags: "#{params[:tags]},#{tag.id})
And then in the controller use
#tags = params[:tags].split(',')

Refinery CMS: Adding custom field to custom engine index page

i have generated a custom engine "Pianos" inside of refinery cms. On the index piano page, I'd like to display the content of a related custom field called "pianos_introduction_text".
What would be the correct way to generate and display this custom field?
Thanks for your help!
Our preference however is to create custom page parts in the normal pages section, rather than to use the copywriting engine.
Under the pages admin section you should see that a page has been added for pianos.
If you add a new page part (or even use an existing one), you can then retrieve and render it from the engine's index page like this:
<%= raw #page.content_for(:new_page_part) %>

How to create permalink pages for individual posts and notes on Tumblr

I'm currently creating my first custom theme for Tumblr and I can't find any explanation of how permalink pages for posts and that post's notes work or how they are written, how do I go about this? what is the code needed and where is it implemented?
Anything you want to appear on a permalink only page you want to have wrapped in block:PermalinkPage tags. If you only want it to show on the index page, wrap it in block:IndexPage tags.
To get your notes to show use {block:PostNotes}{PostNotes}{/block:PostNotes}. This will only appear on a permalink page (despite it not being wrapped in block:PermalinkPage tags. {PostNotes} creates an ol tag with a class of notes for you to style in any way you'd like.

Resources