I used apotonick's cells so that I could embed a regular Rails form (rendered through Rails) in a Radiant page. I found the Radiant extension too limiting.

This is rather involved though. What I like about it is I can embed arbitrarily complex forms without having to leave the Radiant rendering system. I plan to write this up at some point and put it in the Wiki.

At a very high - level, here's how I did that.

1) Install cells 3.3.4 (anything higher is Rails 3 specific)
2) Added these commands so that cells can be processed from within Radiant (I did these in a custom extension (and the pathing reflects that), but you could theoretically do them anywhere in the initialization process, as long as you got the paths right):

    CELL_PATH = "#{File.expand_path(File.dirname(__FILE__))}/app/cells"
CELL_FILES = Dir.glob("#{CELL_PATH}/*_cell.rb").map {|f| File.basename(f)}

    ##Cell configuration
#Modify view paths for ::Cell::Base to include local cell view directories
    ::Cell::Base.view_paths += [CELL_PATH, "#{CELL_PATH}/layouts"]

    #Add CELL_PATH to Rails $LOAD_PATH
    $LOAD_PATH.unshift CELL_PATH

    #Require each Cell class
    CELL_FILES.each {|f| require f}

3) Created cells to render my form.  Here's an example:

  app/cells/question_instances_cell.rb:

class QuestionInstancesCell < ::Cell::Base
  def new
    @question_type = QuestionType.find(@opts['question_type_id'])
    if @question_type.question_type =~ /FAQ/
      @question_instance = QuestionInstance.new()
      view = :edit_faq
    else
@question_instance = QuestionInstance.new(:question => @question_type.questions.first)
      view = :edit
    end

    render :view => view
  end
end

/app/cells/question_instances:

-rw-r--r--@ 1 weyus  staff  568 Aug 30 16:46 edit.html.haml
-rw-r--r--@ 1 weyus  staff  658 Sep  6 13:21 edit_faq.html.haml

/app/cells/question_instances/edit.html.haml:

- type = @question_type.question_type
- form_for [:admin, @question_instance] do |f|
  = f.error_messages
  %p
    = type
    %br/
= f.select :question_id, @question_type.questions.map {|q| [q.question, q.id]}
  %p
= f.label :question_text, "Edit the question to best fit your selling situation"
    %br/
= f.text_area :question_text, :value => @question_type.questions.first.question, :class => 'short'
  %p
= f.label :new_form, "Would you like to build another #{type} question?"
    = check_box_tag :new_form, 1, false
  %p
    = submit_tag 'Submit'

4) Created a custom tag to allow for a cell to be rendered inside of a Radiant template:

    tag "cell" do |tag|
      if tag.attr['name'] && tag.attr['view']
        name, view = tag.attr.delete('name'), tag.attr.delete('view')
tag.locals.page.response.template.controller.render_cell(name, view, tag.attr)
      else
raise TagError.new("`cell' tag must contain `name' and `view' attributes")
      end
    end

5) Call the tag from my Radiant template:

<r:cell name="question_instances" view="new" question_type_id="9"/>

Wes


On 11/12/10 9:34 AM, Marshal Linfoot wrote:
Has anyone used a forms/mailer extension for Radiant that allows a file to be attached and sent with the form? I have a form working -- users fills in text, selects choices from dropdown, checks radio buttons -- everything works, contents emailed successfully. I'm trying to add the capability for people to select a file (photo) from their computer to be attached to the email but I can't get it to work. If you've got it working, could you please share your solution?

Many thanks.
--
marshal

Reply via email to