Rails 3 - help with Atom Feed? - ruby-on-rails-3

Feel like I have been going around in circles, have followed different tutorials all proposing to achieve the promised result and none of them are working for me. I'm using the Kaminari plug-in and my articles are on the index page (6).Can anyone help me with this? Thx
application.html.rb
auto_discovery_link_tag(:atom) # =>
<link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.currenthost.com" />
routes.rb
match '/feed' => 'articles#feed', :as => :feed, :defaults => { :format => 'atom' }
articles_controller.rb
def index
#articles = Article.published.page(params[:page]).per(6).ordered
respond_to do |format|
format.html # index.html.erb
format.atom { #articles = Article.published }
format.xml { render :xml => #articles }
end
end
articles/index.atom.builder
atom_feed do |feed|
feed.title "Title"
feed.updated(#articles.blank? ? Time.now : #articles.first.created_at)
#articles.each do |article|
feed.entry article do |entry|
entry.title article.title
entry.content article.body, :type => 'html'
entry.author do |author|
author.name article.author
end
end
end
end

Your route goes to an action feed but your respond_to block corresponds to the action index. You probably want:
match '/feed' => 'articles#index', :as => :feed, :defaults => { :format => 'atom' }

In your application template (application.html.rb), try this instead:
<%= auto_discovery_link_tag(:atom, feed_path, { :title => "My ATOM Feed" }) %>

Related

Syntax error, but correct syntax? unexpected ':', expecting ')'

I have a nested form (see below)
Model Artist (artist is a user)
has_many :art_works
has_many :canvases
accepts_nested_attributes_for :art_works //artworks is what im currently working on
accepts_nested_attributes_for :canvases
Controller art_works
def new
#artist = Artist.find(params[:artist_id])
#artwork = #artist.art_works.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #artwork }
end
end
def create
#artwork = ArtWork.new(params[:artwork])
respond_to do |format|
if #artwork.save
format.html { redirect_to #artwork, notice: 'artwork was successfully created.' }
format.json { render json: #artwork, status: :created, location: #artwork }
else
format.html { render action: "new" }
format.json { render json: #artwork.errors, status: :unprocessable_entity }
end
end
end
artworks views _form
<%= form_for(#artwork, :url => artist_art_works_path(current_artist) :multipart => true) do |f| %>
<p>
<%= f.file_field :art %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I was pretty positive that this would work, but i'm assuming my :url is incorrect? I'm not really sure what else it would be though. below are my routes for artworks the reason why I am nesting these things is because an artist can upload art into an artwork model the idea is to have several pieces of art in one artwork (like an album has many images inside of it)
artist_art_works GET /artists/:artist_id/art_works(.:format) art_works#index
POST /artists/:artist_id/art_works(.:format) art_works#create
new_artist_art_work GET /artists/:artist_id/art_works/new(.:format) art_works#new
edit_artist_art_work GET /artists/:artist_id/art_works/:id/edit(.:format) art_works#edit
artist_art_work GET /artists/:artist_id/art_works/:id(.:format) art_works#show
PUT /artists/:artist_id/art_works/:id(.:format) art_works#update
DELETE /artists/:artist_id/art_works/:id(.:format) art_works#destroy
Thank you very much in advance for your help. (sorry for the noobness)
You're missing a comma. Yeah the error message isn't that helpful.
#artwork, :url => artist_art_works_path(current_artist) :multipart => true
vs
#artwork, :url => artist_art_works_path(current_artist), :multipart => true

Update action in controller not changing

I am trying to change the status of an attribute called listing_status from Active to Terminated. I tried it like this and it is not working. Any idea what I am doing wrong?
<%=link_to("Terminate", listing_path(listing, :listing_status => "Terminated"), :method => :put, :confirm => "Are you sure you want to TERMINATE this listing?", :class => 'btn btn-danger')%>
Listing Controller Update Action
def update
#listing = Listing.find(params[:id])
respond_to do |format|
if #listing.update_attributes(params[:listing])
if #listing.listing_status == "Active"
#listing.sold.destroy if #listing.sold
end
flash[:notice] = 'house was successfully updated'
format.html { redirect_to :action=> "show" }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
You're passing the status as params[:listing_status], but trying to update the Listing with params[:listing]. Since there's no parameter with that name, no updates will occur.
You probably want to change your link to fit the expected parameter format:
<%= link_to("Terminate",
listings_path(listing, 'listing[listing_status]' => "Terminated"),
:method => :put, :confirm => "Are you sure you want to TERMINATE this listing?",
:class => 'btn btn-danger') %>

The error occurred while evaluating nil.sort_by

In my articles_controller I have the follow definition
def index
#tags = Article.tag_counts_on(:keywords) || ''
klass = Article
klass = klass.tagged_with(#keyword) if (#keyword = params[:keyword]).present?
#articles = klass.paginate(:page => params[:page])
#articles = Article.where(:state => '4').paginate(:page => params[:page], :per_page => 10)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #articles }
end
end
and in my views/articles/index.html.erb I have the code:
<% #tags.sort_by(&:count).reverse.each do |k| %>
<% url_opts = {:action => "index", :controller => "articles"}
link_name = "#{k.name} (#{k.count})" %>
<% if #keyword == k.name %>
<%= link_to link_name, url_opts.merge(:keyword => nil), :class => "tag current_tag", :title => "Click again to see all" %>
<% else %>
<%= link_to link_name, url_opts.merge(:keyword => k.name), :class => "tag", :title => "Click to filter by #{k.name}" %>
<% end %>
<% end %>
I use Ruby 1.9.2, Rails 3.0.11 and in Gemfile the gem act-as-taggable-on and the follow code
rails generate acts_as_taggable_on:migration
create tables tags and taggings
In my logs I have this error:
Rendered articles/index.html.erb within layouts/application (57.4ms)
Completed 500 Internal Server Error in 189ms
ActionView::Template::Error (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.sort_by)
The variable #tags is an Array? and have a nil object?
This error is a common Rails (probably Ruby, actually) confuser that just means #tags was nil when you called the sort_by method ... and I guess the error is trying to be helpful since sort_by is a method of Array.
So, why is #tags nil? Fire up rails console (in your project directory) and execute Article.tag_counts_on('something') -- where 'something' is a keyword.
Perhaps you meant in the first line to get keywords from the params array?
#tags = Article.tag_counts_on(params[:keywords])
Also, you need to handle the case where no tags are found, right?
Finaly the solution for this error for me was: NOT DRY. When add the code from
def index to
def all on articles_controller my problem disapear.
my new articles_controller
def index
#tags = Article.tag_counts_on(:keywords)
klass = Article
klass = klass.tagged_with(#keyword) if (#keyword = params[:keyword]).present?
#articles = klass.where(:state => '4').paginate(:page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #articles }
end
end
def all
#tags = Article.tag_counts_on(:keywords)
klass = Article
klass = klass.tagged_with(#keyword) if (#keyword = params[:keyword]).present?
#articles = klass.where(:state => ['3', '4']).search(params[:search]).order('accepted desc').paginate(:page => params[:page], :per_page => 10)
respond_to do |format|
format.html { render 'index' }
format.xml { render :xml => #articles }
end
end

No Route matches on Form with nested atributes but not default action

I have a form to update an item called post, but i need a different method than update because there are 2 ways to update the post, i have tried this in a lot of ways but i only get this error
No route matches "/topics/1/posts/35/completed"
the controller:
def completed
#post.download_remote_image
respond_to do |format|
if #post.save
format.html { redirect_to topic_path(#topic), :notice => t('.post_created') }
else
format.html { render :action => :edit }
end
end
end
the view part:
= form_for [#topic, #post], :url => completed_topic_post_path(#topic, #post) do |f|
the routes:
resources :topics do
resources :posts do
get 'complete', :as => :complete
post 'completed', :as => :completed
end
end
Thanks !!
:url => completed_topic_post_path(#topic, #post) needs to be :url => topic_post_completed_path(#topic, #post)

Will paginate in rails3

<%= will_paginate #semails, :renderer => 'RemoteLinkRenderer' , :remote => {
:loading => 'loadingPanel.show()',:complete => 'loadingPanel.hide()'} %>
in rails2
how to convert this to rails3
This is the routes using for rails 2 for semails
map.resources :users,
:collection => {:uapload_avatars => :post, :aselect_friend => :get, :alist_friend => :get, :aist_moderator => :get},
:member => {:anew_avatars => :get, :acreate_avatar => :post
} do |user|
user.resources :semails, :collection => { :sort => :get, :asave_draft => :post }
end
how to convert this routes in rails 3 ?
I'm facing this error
will clicking the pagination link it just redirecting to the home page is that routes issue or will paginate issue in rails 3
please help me to solve this issue
I'm not 100% about the spelling but the new routes for rails would approximate something like this:
resources :user do
collection do
post "uapload_avatars"
get "aselect_friend"
end
resources :semails do
collection do
get "sort"
post "asave_draft"
end
end
end
http://guides.rubyonrails.org/routing.html