wrong number of arguments (2 for 1) - ruby-on-rails-3

What is this error?
ArgumentError in BrandsController#index
wrong number of arguments (2 for 1)
My code for index in controller is:
def index
#brands = Brand.all
##brands = Brand.paginate :page => params[:page],:per_page => 6
respond_to do |format|
format.html
format.xml { render :xml => #brands.to_xml }
end
end
My code for view of index.html.haml is:
- content_for :title do
Listing Brands
- content_for :div do
Listing Brands
- content_for :subnav do
#navigation-search
/= yield :brand_img
%img{:src => "images/lifestyle.gif", :height => "35"}
/= link_to 'Change Brand', new_brand_path
%a.Metro-16{:href => new_brand_path, :title => "Change brand"} Change Brand
= render :partial => 'updatemesg'
- content_for :brand_img do
%img{:src => "images/lifestyle.gif", :height => "35"}
-#
- content_for :subnav do
#navigation-search
%form
#{yield :brand_img}
%ul
%li
%a.tt-top-center{:title => "Total Events"}#{Event.all.size}
%span Total events
%li
%a.blue.tt-top-center{:title => "Today's Events"} 12
%span Today's Events
%li
%a.green.tt-top-center{:title => "Tomorrow's Events"} 3
%span Tomorrow's Events
%li
%a.tt-top-center{:title => "Upcoming Events"} 7
%span Upcoming Events
%a#show-modal.create-24.tt-top-center{:href => new_brand_path, :title => "New Brand"} New Brand
- #brands.each do |brand|
.ovr
%ol.hoverbox
%li
= link_to image_tag(brand.photo.url, :alt => brand.name, :class => "preview"), brand_path(brand)
/= link_to image_tag(brand.photo.url), { :class => "preview"}
.abc
= brand.name
I am not getting why this error is coming
ArgumentError in BrandsController#index
wrong number of arguments (2 for 1)

Related

Routes error on nested controller when submitting form, databasedotcom-gem

I am using the databasedotcom & databasedotcom-rails gem so that I get generate leads into Salesforce from form submissions. It was working standalone until I nested the controller under a parent, and now when I press submit I get this routes error:
No route matches [POST] "/events/516ee9a0421aa9c44e000001/leads/new"
Here is all my code:
resources :events do
resources :leads
end
class LeadsController < ApplicationController
include Databasedotcom::Rails::Controller
def new
#lead = Lead.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #lead }
end
end
def create
#lead = Lead.new(params[:lead])
#lead.event_id = params[:event_id]
#lead['OwnerId'] = '005b0000000WxqE'
#lead['IsConverted'] = false
#lead['IsUnreadByOwner'] = false
respond_to do |format|
if #event_lead.save
format.html { redirect_to #lead, :notice => 'Lead was successfully created.' }
format.json { render :json => #lead, :status => :created, :location => #lead }
else
format.html { render :action => "new" }
format.json { render :json => #lead.errors, :status => :unprocessable_entity }
end
end
end
end
<%= simple_form_for [#event, #lead], url: new_event_lead_path do |f| %>
<%= f.input :Download_Brochure__c, :check => "true", :as => :boolean, :as => :hidden %>
<%= f.input :FirstName %>
<%= f.input :LastName %>
<%= f.input :Company %>
<p>Also interested in:</p>
<%= f.input :Sponsor_Request__c, :as => :boolean, :label => "Sponsoring" %>
<%= f.input :Presenting__c, :as => :boolean, :label => "Presenting" %>
<%= f.input :Newsletter_Signup__c, :as => :boolean, :label => "Newletter" %>
<%= f.input :Privacy_Policy__c, :as => :boolean, :checked => true, :label => "Would you like to stay updated" %>
<%= f.button :submit, :label => "Submit" %>
<% end %>
The problem is in your form. With the routes as you have written them, the new_event_lead_path maps to a GET request to your LeadsController#new action. Run rake routes on the command line to confirm this.
You want to submit the form to LeadsController#create. Rails will set this up for you when you use a new instance of Lead in the expression simple_form_for [#event, #lead] provided you don't override the url. Therefore, update your form:
<%= simple_form_for [#event, #lead] do |f| %>

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

Adding name value with link_to

I'm using a link_to to create an object in my Rails 3 app. Searching has given me the proper way to use link_to with the :post method, but I'm wondering if using link_to to pass in a name value for my object as well. Here is my link:
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :method => :post}, :class => "button white" %>
My profiles_controller.rb:
def create_inside
#todo = Insidetodo.new
#todo.save!
if #todo.save
redirect_to #profile.todo, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
My todo.rb model:
class Todo < ActiveRecord::Base
has_and_belongs_to_many :profiles
validates :name, :presence => true
end
Is there a way to add in :name => "#{#user.profile.todotext}" to the link_to so that it passes and saves? I don't know if it's creating properly because at the moment when I click a link_to I get a validation error - Validation failed: Name can't be blank.
For passing name in the link_to
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :name => "#{#user.profile.todotext}", :method => :post}, :class => "button white" %>
and the controller must be
def create_inside
#todo = Insidetodo.new(:name => params[:name])
if #todo.save
redirect_to #todo.goal, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
But the link_to will pass the name parameter in url only(like Todo text).
If you want the name not to be sent in the url, you might want to use a form and use a submit button instead of the link as it is representing an action and not a link.
<%= form_tag(:controller => "profile", :action => "create_profile") do -%>
<%= hidden_field_tag :name, #user.profile.todotext %>
<%= submit_tag "Todo Text" %>
<% end -%>

how do specfiy :with attribute in rails 3 link_to

This is link_to_remote code for rails 2.3.8
link_to_remote "Click here",
:update=>'flowtracker',
:with=>"'topcount='+$('topcount').value,
:url=>{
:controller => 'sessions',
:action => 'session_for_tracker',
:layout=>1
}
link_to_remote deprecated in rails 3
so how can i specify the :with attribute in link_to rails 3
And i'm using prototype.js
Use link_to helper with :remote => true option and do whatever you want in your js view file to your action.
EXAMPLE
in view:
<%= link_to 'ajax call', :controller => "sessions", :action => "session_for_tracker", :remote => true %>
in controller:
def session_for_tracker
#topcount = 1000; #get actual value here
respond_to do |format|
format.js
end
end
in js view for action:
page << %|
$('flowtracker').update("#{#topcount}");
|

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