Undefined method `each 'for" # <Complaigns :: ActiveRecord_Relation: 0x00000003cfb4c8> ": String
I have a model called Complaign, with some other attributes along with the declaration date ( c_date).
In ComplaignControllerI have a view indexthat displays everything complaigns. There is a filter that takes from date to today. By filtering this out, it works great and correctly displays the actions launched on those dates.
Now I want the result of this query to be passed to another method, such as an export method.
I thought of passing this from an index view, as it is stored in @complaigns.
This is my index method:
def index
if params[:from] && params[:to]
from = params[:from].to_date
to = params[:to].to_date
@complaigns = Complaigns.where(:c_date => from..to)
else
@complaigns = Complaigns.all
end
end
In the index view, this is what I wrote
<%= link_to "Export", {:controller => "complaigns", :action => "export", :complaigns => @complaigns}%>
This is the export method.
def export
@complaigns = params[:complaigns]
end
, , :
@complaigns.each, -
undefined method `each' for "#<Complaign::ActiveRecord_Relation:0x00000003cfb4c8>":String
, , , String each. , String to Complaign export , , "" String? ?
HTTP - , , ( cookie ). export index, . , :
<%= link_to "Export", {:controller => "complaigns", :action => "export", :from => params[:from], :to => params[:to] }%>
def export
if params[:from] && params[:to]
from = params[:from].to_date
to = params[:to].to_date
@complaigns = Complaigns.where(:c_date => from..to)
else
@complaigns = Complaigns.all
end
end
index .
DRY. :
def index
if params[:from] && params[:to]
from = params[:from].to_date
to = params[:to].to_date
@complaigns = Complaigns.where(:c_date => from..to)
else
@complaigns = Complaigns.all
end
if params[:export].present?
render 'export'
else
render 'index'
end
end
:
<%= link_to "Export", {:controller => "complaigns", :action => "index", :from => params[:from], :to => params[:to], :export => true }%>
PS. . .