views:

148

answers:

3

I want to do store the parameters from a form in a model. Since I don't want the model to use any database, it does not inherit from ActiveRecord::Base. I'm thinking it should look something like this:

# in view:
<% form_for :question, :url => {:action => "ask"} do |f| %>
  <%= f.text_field(:q) %>
  <%= submit_tag %>
<% end %>

# in controller:
def ask
  # I want this to magically set all variables in @question using 
  # values from params.
  @question = params[:question]
end

# in model:
class Question
  attr_accessor :q

  def initialize
    @q = ""
  end
end

But after spending 1½ days on it, it doesn't seem to be the right way to do it. Any suggestions would be much appreciated.

A: 

Even if you set your Question properly, how do you plan to persist this? A file?

I think it is a much better approach to get a deep understanding of ActiveRecord before going for fancy models that have custom persistence

Sam Saffron
Short answer: I don't. I only want to create the Question object and use it as a parameter for a method call. It would be simpler if I could just pass on the hash, but I don't have that option. I have a method that expects a Question and I have a form that gives me all I need to create a Question, I just don't know how to connect the two.
A: 

Take a look at this article:

http://pullmonkey.com/2008/1/6/convert-a-ruby-hash-into-a-class-object

It shows how to create a class that will dynamically create a class from the passed in Hash.

Josiah I.
Just what I was looking for. Thanks!
A: 

You might want to check out Ryan Bates' Railscast on creating a non ActiveRecord model http://railscasts.com/episodes/121-non-active-record-model

... however I'd suggest that if you're thinking RESTfully about this, it sounds from your comment to Sam's answer like you may have another RESTful resource at work - i.e. you don't actually want to use a QuestionsController... but instead something to do with what you're actually creating (the method call you mention). You can still initialize your Question object as part of that process.

mylescarrick