": String I have a model called Complaign, with some oth...">

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? ?

+4
4

Ruby on Rails , , . HTTP/Ruby on Rails . , :

<%= link_to "Export", {:controller => "complaigns", :action => "export", :complaigns => @complaigns.map(&:id)}%>

def export
         @complaigns = Complaigns.find(params[:complaigns])
end
+1

, @complaigs active_record. @camplaigs , .

@complaigns.collect(&:id)
0

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. . .

0

, . , HTML , @complaigns ( , @complaigns.to_s).

, , , , @camplaigns . :

:

<%= link_to "Export", {:controller => "complaigns", :action => "export", :to => params[:to], :from => params[:from]}%>

:

def export
  index
end
0

Source: https://habr.com/ru/post/1598164/


All Articles