Hi all, On several projects where I''ve used rails I''ve hitten the same wall: I want to use the form helpers, validation, etc but I don''t want to persist the data at all.. Simple example: I have a list of available reports that can be run (represented by a ''reports'' table and associated model). If the user clicks on a report to run it I need to ask them a bunch of questions before it runs.. and that''s a great place to use the form helpers and validation.. but as far as I can tell I can''t!! When I attempt to just create classes and use the helpers with them they fail.. and if I try and dynamically add fields to a class that aren''t persisted they also cannot be accessed by the helpers... Is this a common problem? How have others gotten around it? I''ve hit this MANY times.. for example: complex filters (where a user can specify multiple filters and keep adding more).. I always end up doing everything manually to get around it. Any help would be greatly appreciated!
> Any help would be greatly appreciated!http://wiki.rubyonrails.com/rails/show/HowToUseValidationsWithoutExtendingActiveRecord This was posted a few days ago, I use it quite a bit and it works really, really well. -- Cheers Koz
Sweet.. so we''re half way there!! What does it take to use a class in the Form Helpers? --- Michael Koziarski <koziarski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Any help would be greatly appreciated! > >http://wiki.rubyonrails.com/rails/show/HowToUseValidationsWithoutExtendingActiveRecord> > This was posted a few days ago, I use it quite a > bit and it works > really, really well. > > -- > Cheers > > Koz > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 7/23/05, Luke Galea <luke.galea-bJEeYj9oJeDQT0dZR+AlfA@public.gmane.org> wrote:> Sweet.. so we''re half way there!! > > What does it take to use a class in the Form Helpers?You need to define an attributes= methods something like this: def attributes=(attrs) attrs.each do |key, value| self.send("#{key}=", value) end end Note, that does no error handling or anything, but it should get you started. -- Cheers Koz
Thanks. I''m wondering: Is this not a common requirement? Why isn''t something like this a standard part of ROR? I''m picturing some other base class to extend in ActiveRecord (other than Base) that allows for transient objects... --- Michael Koziarski <koziarski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 7/23/05, Luke Galea <luke.galea-bJEeYj9oJeDQT0dZR+AlfA@public.gmane.org> > wrote: > > Sweet.. so we''re half way there!! > > > > What does it take to use a class in the Form > Helpers? > > You need to define an attributes= methods something > like this: > > def attributes=(attrs) > attrs.each do |key, value| > self.send("#{key}=", value) > end > end > > Note, that does no error handling or anything, but > it should get you started. > > > -- > Cheers > > Koz >
Thanks again for the help.. But it doesn''t seem to be
working!
I''ve defined a class ReportRun: 
class ReportRun
  def initialize
    @format = ReportEngine::DefaultFormat
  end
  
  def initialize( attrs )
    attributes=( attrs )
    self
  end
  
  def attributes=(attrs)
    attrs.each do |key, value|
      self.send("#{key}=", value)
    end
  end
end
And I have my report controller such that when the
user clicks run on the report it asks them a bunch of
questions (let''s start with the format to begin
with).. so reports_controller has: 
# Ask the user questions about the report
def run
  @report = Report.find(@params[:id])
  @report_run = ReportRun.new()
end
# Run the report
def execute_report
  @report = Report.find(@params[:id])
  @report_run = ReportRun.new( @params[:report_run] )
  # TODO: Run the actual report
end
Where it all goes wrong is in run.rhtml where I
attempt to address the @report_run (using a text_field
here for simplicity but it will be a select):
<%= start_form_tag :action => ''execute_report'', :id
=>
@report %>
  <p><label for="report_run_format">Format:</label>
  <%= text_field ''report_run'', ''format''
%></p>
  
  <%= submit_tag "Run" %>
<%= end_form_tag %>
The error is: "too few arguments" on the line
text_field ''report_run'', ''format''
I know that if I change report_run to report (and give
report a format attribute) it works perfectly.. so it
can''t really be "too few arguments"!!
Any help would be greatly appreciated!!
--- Michael Koziarski <koziarski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
 > You need to define an attributes= methods something
> like this:
> 
> def attributes=(attrs)
>   attrs.each do |key, value|
>     self.send("#{key}=", value)
>   end
> end
> 
> Note, that does no error handling or anything,  but
> it should get you started.