I'm pretty new to Rails 3, and I'm trying to make an RSS/Atom feed. I know about auto_discovery_link_tag, but what is the associated controller/action supposed to look like?
Thanks!
Auto_discovery_link_tag is a good start. A quick Google search and I found blog posts on How to Create an RSS feed in Rails. Let me fill you in on what your associated controller/action is supposed to look like:
controllers/posts_controller.rb
def feed
#posts = Post.all(:select => "title, author, id, content, posted_at", :order => "posted_at DESC", :limit => 20)
respond_to do |format|
format.html
format.rss { render :layout => false } #index.rss.builder
end
end
The name of this file should match the controller. See, below:
views/posts/feed.rss.builder
xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0" do
xml.channel do
xml.title "Your Blog Title"
xml.description "A blog about software and chocolate"
xml.link posts_url
for post in #posts
xml.item do
xml.title post.title
xml.description post.content
xml.pubDate post.posted_at.to_s(:rfc822)
xml.link post_url(post)
xml.guid post_url(post)
end
end
end
end
This is where all the Railsy magic happens. Here, the RSS feed XML is generated and returned to HTTP.
Using the auto_discovery_link_tag:
In the controller:
respond_to do |format|
format.html
format.atom {render action: 'index', layout: false}
end
Related
I have this two methods on catalog_controler.rb
def latest
#boxes = Box.latest 5
#page_title = 'Novedades'
end
def rss
latest
render :layout => false
end
end
on app/views/catalog folder I have this xml file , rss.xml.erb
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
xml.channel do
xml.title #page_title
xml.link(url_for(:action => "index", :only_path => false))
xml.language "en-us"
xml.ttl "40"
xml.description "International Boxes"
for book in #books
xml.item do
xml.title(book.title)
xml.description("#{box.model})
xml.pubDate(book.created_at.to_s(:long))
xml.guid(url_for(:action => "show", :id => book, :only_path => false))
xml.link(url_for(:action => "show", :id => book, :only_path => false))
end
end
end
end
But it gives an missing template error:
Missing template catalog/rss, application/rss with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/user/desktop/eshop_con_CSS/app/views"
How can I show the xml file on browser?
Read about MimeResponds's respond_to method. It should look like below, please change the block content as per your requirement.
def rss
#latest = latest
respond_to do |format|
format.xml { render xml: #latest }
end
end
respond_to can adopt to various different MIMES including popular .json, which is very handy in building/working-with APIs.
I want to send the count value,for count the post i am using thumbs up gem in ROR.
Now i want to send the count in json,the vote as post action in def show
def index
#posts = Post.all
respond_with(#posts) do |format|
format.json { render json: #post_names = {:post => #posts.as_json(:only=> [:content, :title]) } }
end
end
I want to send the count value using json,because i want to show that count value in my client side.
You can send vote count by this way. I assume something like this can work..
def index
#posts = Post.all
respond_with(#posts) do |format|
format.json { render :json => #post_names = {:post => #posts.map {|t| {:title => t.title, :content => t.content, :count => t.votes_for }} } }
end
end
Since you're working on the controller, you might find it helpful to look at the render section of the Rails documentation.
I'm using sunpost gem for search in my rails project.
I have now two languages in my app:
I18n.default_locale = :en
LANGUAGES = [
['English',
'en'],
["EspaƱol".html_safe, 'es']
]
I have in my post.rb model, a language attribute that contains the value "es" for spanish language or value "en" for english language.
I have in posts_controller in index action the next method:
def index
#search = Post.solr_search do |s|
s.fulltext params[:search]
s.keywords params[:search]
s.order_by :created_at, :desc
s.paginate :page => params[:page], :per_page => 20
end
#posts = #search.results
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
format.js
end
end
I get the current language with I18n.locale.to_s I get with this code "es" or "en"
My question is: How can I only show the results for the language currently in use by user in my website?
Thank you very much!
It would be very helpful if you could post the searchable block in the post model. But until then, I will take a stab at it.
Your Post model should look something like the following:
class Post < ActiveRecord::Base
searchable do
...
string :language
...
end
end
Where you are indexing the language the post is written/stored in.
Then your controller you use the language field as a filter. It should look like:
def index
#search = Post.search do |s|
s.keywords params[:search]
s.with(:language, I18n.locale.to_s) if I18n.locale.present?
s.order_by :created_at, :desc
s.paginate :page => params[:page], :per_page => 20
end
#posts = #search.results
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
format.js
end
end
and there you have it!
I recently changed the pagination with will_paginate in my Rails (2.3.4) app to use Ajax for the pagination and records per page. The pagination was done using the method described here: http://github.com/mislav/will_paginate/wiki/Ajax-pagination
I'm using this code in my view:
Records per page: <%= select_tag :per_page, options_for_select([4,8,24,100,500], #per_page.to_i), :onchange => remote_function(:url => users_path, :method => :get, :with => "'per_page=' + this.getValue()") %>
This works fine if I'm not viewing search results. But if I do a search and them attempt to change the records-per-page, my search results are lost and all records are returned. I'm pretty sure this is due to the url I'm calling in my remote_function, but I don't know how to fix it.
This is the code in my controller:
def index
#search = User.search(params[:search])
#search.order||="ascend_by_last_name"
if #search.count > 0
#users = #search.paginate(:page => params[:page], :per_page => users_per_page )
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #users }
format.csv { send_data #users.to_csv }
format.js {
render :update do |page|
# 'page.replace' will replace full "results" block...works for this example
# 'page.replace_html' will replace "results" inner html...useful elsewhere
page.replace 'results', :partial => 'userview'
end
}
end
else
flash[:notice] = "Sorry, your search didn't return any results."
redirect_to users_path
end
end
Any help would be appreciated! Thanks.
You can append the per_page param to the end of the current query string.
:with => "'#{request.query_string}&per_page=' + this.getValue()"
This assumes there is a current query string, which could cause issue.
Everything I am reading about rails 3 and AJAX says that we should have
respond_to do |format|
#wines = Wine.all(:conditions => {:category => "Sparkling"})
format.js
end
and then a seperate js.erb file that is
$("wines").update("<%= escape_javascript(render :partial => "sparkling")%>")
but that one line js file seems a little extreme, can I do something like this:
respond_to do |format|
#wines = Wine.all(:conditions => {:category => "Sparkling"})
format.js {render :js => '$("wines").update("<%= escape_javascript(render :partial => "sparkling")%>"')}
end
and then leave out the extra .js.erb file
The problem I see here is a double render (am noob so I'm not sure)? What is the best way to do this?
Inline RJS is a bad practice, but you can use it like this:
def your_action
#wines = Wine.all(:conditions => {:category => "Sparkling"})
respond_to do |format|
format.js {render :update do |page|
page << '$("wines").update("<%= escape_javascript(render :partial => "sparkling")%>"')
end}
end
end
UPD
No, it's not silly to store one more file. It makes your controllers cleaner. Look with your_action.js.erb
# your controller
def your_action
#wines = Wine.all(:conditions => {:category => "Sparkling"})
end
# your you_action.js.erb
$("wines").update("<%= escape_javascript(render :partial => "sparkling")%>"
That is two lines instead of 6 :)