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.
Related
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.
I am using the en.yml file to control the text on labels and buttons in my rails application with the following structure:
helpers:
submit:
person:
create: Create Person
label:
log:
comment: Enter your comments here
In this case I do not have to invoke the translate method explicitly. Is there a way to have similar translation on an attribute of the element? I have searched for the hierarchy of en.yml without success so far. So, I don't even know if it's possible.
Here is an example:
The view has:
<input id="person_name" hover_hint = "Please enter your name" type = "text" name="person_name">
Is there a way to manipulate the content of hover_hint attribute via en.yml without using translate method explicitly?
I can always create a symbol :hover_hint and then use t :hover_hint in the view for the value of hover_hint attribute. But is there a way to avoid that - explicitly using the translate method- and use the en.yml structure instead as is done for labels and buttons?
I'm working on a pastebin clone. I need the user to be able to type in HTML without it being used like HTML. For example, my user types "<html> Hello, world! </html>", and the html tags don't appear because the text is being treated like HTML. I do not want this to happen.
I want this to happen to this line:
<%=simple_format(#post.content )%>
How could I accomplish this? I tried using raw and .html_safe and they didn't work.
The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements. These helper methods extend Action View making them callable within your template files.
http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
I'm doing maintenance on an old rails app, and my rails wasn't that great to begin with. I'm seeing the following code all over in forms in views; this example is from a payment/billing screen, so the fields are common billing fields like address fields:
<%= t : "front.City" %>
This is used for the form field label. I can't figure out what either the 't' is, or the 'front'. I'm trying to copy this partial for a different payment method. When I changed the 'front.new-label', it broke something because it then displayed the label as
en, front, new-label
I'm wondering if the 't' is some kind of helper method, like <%=h is to sanitize output. This is used in a partial, so I looked in the layout that includes the partial to see if 'front' was defined anywhere in there but didn't find anything. This 't' and 'front' are used all over the app as labels, so I thought it just had something to do with styling. But it's used so often that I can't see all these front.variables being defined somewhere, but then why does it break when I change it?
You're correct, t is a short-cut for the i18n translation method:
http://railsapi.com/doc/rails-v2.3.8/classes/ActionController/Translation.html#M001880
Have a read of the internationalisation guide to get a feel for what's going on.
I am using ActiveScaffold in a Ruby on Rails app, and have replaced the default "actions" text in the table (ie. "edit", "delete", "show") with icons using CSS. I have also added a couple of custom actions with action_link.add ("move" and "copy").
For clarity, I would like to have the icons displayed in a different order than they are. Specifically, I would like "edit" to be the first icon displayed.
I seem to be able to change the order of the action_links by the changing the order of definition in the controller. I have also been able to change the order of the default actions by first config.actions.excluding everything, and then adding them with config.actions.add in a specific order.
However, my custom actions always seem to appear before the default actions in the list.
Ideally I would like them to display "edit" "copy" "move" "delete" (ie - built-in, custom, custom, built-in). Can anyone suggest how I might do this?
One idea I had was to re-define "edit" as a custom action (with the default functionality), but I don't know how to go about this either.
Caveat: I don't know ActiveScaffold. This answer is based on me reading its source code.
It looks like the action_links variable is a custom data structure, called ActionLinks. It's defined in ActiveScaffold::DataStructures.
Internally, it has a #set variable, which is not a Set at all, but an Array. ActionLinks has an add, delete, and each methods that serve as gatekeepers of this #set variable.
When displaying the links, ActiveScaffold does this (in _list_actions.rhtml):
<% active_scaffold_config.action_links.each :record do |link| -%>
# Displays the link (code removed for brevity)
<% end -%>
So, short of extending ActiveScaffold::DataStructures::ActionLinks to add a method to sort the values in #set differently, there doesn't seem to be a way to do it, at least not generally.
If I were you, I'd add something called order_by!, where you pass it an array of symbols, with the proper order, and it resorts #set. That way, you can call it after you're done adding your custom actions.