Trying to remove the ID, timestamps and other information below the titles - ruby-on-rails-3

enter image description here
I only want the titles of the book and not the text below it. I tried looking if I am displaying anything other than the title but I can't seem to find it.
This is my index.html.erb file.
<%= #books.each do |book| %>
<h2><%= book.title %></h2>
<% end %>

Related

Middleman App, sort blog posts by month

I'm using middlemanapp to create a blog. I'm trying to output an archive of blog posts sorted by month and year to display in a sidebar. eg. April 2010, May 2010, June 2010, with clickable links to a archive.
So far have this code below which will output the month in number form (eg. July is being output as 7) and I need to have a list that is displayed by month as shown above.
<% blog.articles.group_by {|a| a.date.month }.each do |month, articles| %>
<li><%= link_to month, blog_year_path(month) %> </a></li>
<% end %>
Can someone help, I'm even not certain if middleman offers this functionality, but I'm not very familiar with ruby.
I couldn't find an easy built-in way to do this with Middleman either, but the following will give you a nested list of years and months, with the relevant links:
<ul>
<% blog.articles.group_by {|y| y.date.year }.each do |year, articles| %>
<li>
<a href="<%= blog_year_path(year) %>">
<%= year %>
</a>
<ul>
<% articles.group_by {|a| a.date.month}.each do |month, month_articles| %>
<li><%= link_to month_articles.first.date.strftime("%B"), blog_month_path(year, month) %></li>
<% end %>
</ul>
<% end %>
</li>
</ul>
e.g.
2013
August
July
June
...
(I'm fairly sure I borrowed the above from this Middleman template on Github, but if not it was found via a Github search for "blog.articles.group_by month".)

Show value of activity.trackable with public_activity Rails gem

I'm using the public_activity gem to track changes with my Reposts model, which contains a comment_id and belongs_to the Comments table. In the _create.html.erb partial for the create action of a Repost, I can use this code with no errors:
<%= activity.trackable.comment %>
and the view will display this text:
Comment:0x00000004508ee0>
Which proves that it's displaying the Activerecord using the relationship I established between Reposts and Comments. However, once I try to extend the code to show the content field on the Comments table I get an error. For example:
<%= activity.trackable.comment.content %>
Returns the following error:
undefined method 'content' for nil:NilClass
activity.trackable.comment seems to pull the right record. How can I extend this so not only does it pull the record via the established relationship, but that it also pulls another field from that table?
Thanks!
EDIT 1: Full view for views/public_activity/_create.html.erb
<% if activity.trackable %>
<%= link_to activity.trackable.comment, activity.trackable.comment %>
<% else %>
which has since been removed
<% end %>
EDIT 2: Per Leo's assistance, all I had to do was check for nil. Here's what I had to change my code to:
<% if activity.trackable && activity.trackable.comment.present? %>
<%= link_to activity.trackable.comment.content, activity.trackable.comment %>
<% else %>
which has since been removed
<% end %>
I highly recommend adding that additional code to check for nil if you followed along the public_activity Railscast like I did.
So it seems per this particular question, the activity.trackable.comment was returning a NilClass object for some activities causing the exception to be thrown.
The fix of course is to check to make sure comment is not nil by doing the following
<% if activity.trackable && activity.trackable.comment.present? %>
<%= link_to activity.trackable.comment.content, activity.trackable.comment %>
<% else %>
which has since been removed
<% end %>
It seems like you're accessing the wrong object to get a count from. I've used the impressionist_count method for this sort of task in the past, the documentation here might help you.

How to display text in erb file?

I started rails a week ago, I want to know that how to display an unmodifiable text using erb file,
<%= f.label :email %>
<%= f.text_field :email %>
Here, instead of a text-box, I need a simple text displaying email id.
Any clues?
By "unmodifiable text" in a text field, I'm assuming you don't just want to have the email displayed on screen (if so, then Yuri Barbashov is correct) but also want it inside an input text field, but be unmodifiable.
In that case, you might be best off using javascript. Have a look at this StackOverflow answer for some JQuery examples.
Edit:
Actually, the solution here may be easier if you never plan on re-enabling the text field:
<%= f.text_field :email, disabled: true %>
<%= #your_model.email %>
But to be more precise you have to show your view code

How to make a search results page with pg_search multisearch that has links to the results?

I finally figured out how to implement pg_search's multisearch feature. But I'm having trouble making a usable search results page that displays links back to the various articles and faqs that contain the search terms. It's a pretty basic setup using Rails 3.2.3:
Models:
I have an Articles and a Faqs model, both with "Title" and "Content" attributes, and this code in both models:
include PgSearch
multisearchable :against => [:title, :content]
Search Form View Code:
The search form passes everything to a controller called "results."
<%= form_tag result_index_path, method: :get do %>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "GO", name: nil %>
<% end %>
Results Controller:
class ResultController < ApplicationController
def index
#pg_search_documents = PgSearch.multisearch(params[:query])
end
end
I would like to make a search results page that displays the title of each result found, with a link back to that item. I figured out how to pull the 'class' attribute out #pg_search_documents. My thinking is to do something like this on the results page:
<ul>
<% #pg_search_documents.each do |pg_search_document| %>
<li><%= link_to pg_search_document.searchable.title, "../#{pg_search_document.searchable.class}/#{pg_search_document.searchable.id}" %></li>
<% end %>
</ul>
An example link that this code yields is: http://localhost/Article/3. If I could figure out how to downcase and pluralize "pg_search_document.searchable.class", I'd be home free. I've tried writing various methods in the model and controller, and tried writing a helper. But this is where my Rails skills fail me.
Any suggestions? Anybody know of a more elegant way of accomplishing this? Any ideas / suggestions are very much appreciated.
I did something similar and just used
<%= link_to pg_search_document.searchable.title, pg_search_document.searchable %>
to let Rails dynamically create the path to the associated record.
Well, it's amazing what walking away from the problem for a little while does. That, and more persistent Googling on basic Ruby. Here's the solution I came up with:
<ul>
<% #pg_search_documents.each do |pg_search_document| %>
<li><%= link_to pg_search_document.searchable.title, "../#{(pg_search_document.searchable.class).to_s.downcase!.pluralize}/#{pg_search_document.searchable.id}" %></li>
<% end %>
</ul>
Still, this seems ugly to me. I'd still be very interested to see something more streamlined and intelligent.

tiny mce display tags in rails 3

I am trying to store content of tinyMCE into "detail" coloumn.
Now when I display the content it displays wit all the <p> tags <i> tags etc.
This Is a security feature in rails3 .
But I don't want the <p> tags to be displayed , I want it to be rendered as HTML.
One way I found was <%= something.detail.html_safe %>
the other way I thought was to create a function in model like
def detail_safe
return self.detail.html_safe
end
and display using <%= something.detail_safe %>
Either ways I need to change the <%= %> tag at many places. Is there an easier solution? Or should I manually change at every place?
Thank you.
In the model:
def detail
self[:detail].html_safe if self[:detail]
end
Please note that you will always get html_safe output in this case when you do model_object.detail.
Not matter how you do it, you will have to change all of your <%= %>.
Your options are:
<%= something.detail_safe %>
<%= something.detail.html_safe %>
<%= raw something.detail %>
The only other option I can think of is turning off XSS protection - but don't do that!