Error in rails3 in the controller file - ruby-on-rails-3

This is the code in the controller i am getting error pls help me
class HomeController < ApplicationController
respond_to :json
def index
#home = Home.all
respond_with(#home)
cookies['AFID'] = {
:value => request.env['AFID'],
:expires => 1.year.from_now
}
cookies['SID'] = {
:value => request.env['SID'],
:expires => 1.year.from_now
}
#cookies[:AFID] = { :value =>}
end
def create
#homes = Home.create(params[:home])
respond_with(#home, :location => users_url)
end
end
I got following error
NameError in HomeController#index

First of all, please use the Ruby convention of code indention!
And how about fixing : value => request.env['AFID'], to :value => request.env['AFID'],? Does this help?

Related

rails actionmailer error with 'part' method

I'm upgrading rails 2.3.2 app ot rails 3.
Have unknown error with sending email message in MailerFormError.
MailerFormError is my model: class MailerFormError < ActionMailer::Base
At 1st I have error with 'deliver_send' method (undefined method `deliver_sent' for MailerFormError:Class),
I change it to 'send'. Now I have this:
NoMethodError in LeadsController#create
undefined method `part' for #
My code in controller:
#msg = {}
#msg["errors"] = #lead.errors
#msg["params"] = params
#MailerFormError.deliver_sent(#msg)
MailerFormError.sent(#msg)
This is my class with sending method:
def sent(msg, sent_at = Time.now)
#subject = ("Ошибка при заполнении формы").force_encoding('iso-8859-1').encode('utf-8')
#recipients = 'mymail#gmail.com'
#from = 'mymail#gmail.com'
#sent_on = sent_at
#headers = {}
part( :content_type => "multipart/alternative" ) do |p|
p.part :content_type => "text/plain",
:body => render_message("sent.plain.erb", :msg=>msg )
end
end
1) for Rails 3, to send your notification in your controller , you have to write this :
MailerFormError.sent(#msg).deliver
2) And you have to rewrite your 'sent' method in the Rails 3 way :
def sent(msg, sent_at = Time.now)
...
mail(:to => '...', :from => '...', :subject => '...') do |format|
format.html
format.text
end
...
end
You can also create the text version and html in your view directory app/views/mail_form_error : sent.text.erb and sent.html.erb

Draper: How to convert all allowed attributes and public methods of the decorated model into json

I'm looking for an easy way to convert decorated model into json to use in my client-side templates. I'd like to find a solution where all allowed attributes and public methods of the decorated model will persist in json.
Currently I have backbone RIA with rails backend. I'm using haml_coffee_assets gem for client-side templating. Draper is there to provide decoration for my User model
class ContactDecorator < Draper::Base
include Draper::LazyHelpers
decorates :user
allows :login, :id, :total_unread, :total_messages
def for_json
{
:new_messages => new_messages,
:avatar_link => avatar_link,
:login_name_link => login_name_link,
:id => model.id,
:name => model.login,
:avatar => model.avatar.url(:small),
:humanized_messages_number =>
}
end
def humanized_messages_number
pluralize(user['total_messages'], t("share_my_trip.messages.Message"), t("share_my_trip.messages.Messages")) + " #{new_messages}"
end
def new_messages
model['total_unread'].to_i > 0 ? "(#{model['total_unread'].to_i} #{t("share_my_trip.messages.new")})" : ''
end
def avatar_link
link_to(image_tag(model.avatar.url(:small), :size => "32x32", :onerror => "this.src='/avatars/original/missing.png'"), share_my_trip_user_path(:id => model.login), :id => "user-nick-#{model.id}", :class => "author")
end
def login_name_link
link_to(model.login, share_my_trip_user_path(:id => model.login), :id => "user-nick-#{model.id}", :class => "author")
end
end
my controller code looks like:
def index
#dialogs = ContactDecorator.decorate(current_user.contacts).collect{|c| c.for_json}
end
and than in my view I'm just initializing backbone app:
:javascript
App.init({dialogs: #{#dialogs.to_json}})
Is there a way to delete ugly #for_json decorator method?

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

406 Not Acceptable error while rendering a json view in rails 3.1

My controller:
def index
#feed_items = get_network_feed(current_user)
respond_to do |f|
f.json { render :json => #feed_items, :content_type => "application/json" }
end
end
My view: #index.json.erb
{
Items: <%= #feed_items.to_json.html_safe %>
}
I get a 406 Not acceptable error. Can anyone tell me what I missed here?
Thanks,
pR
Try following code in your controller, I think it will solve your problem!
def index
#feed_items = get_network_feed(current_user)
render :json => #feed_items, :content_type => "application/json"
end

Rails 3 - help with Atom Feed?

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" }) %>