3

I'm trying to use the Rails Cell Gem (https://github.com/apotonick/cells), but I'm having some trouble rendering a cell from a controller and a view.

This is my cell (simplified):

class AcquiredSkillsCell < Cell::ViewModel
  def show
    render
  end

  def has_acquired_skills?
    model.count > 0
  end
end

and within a standard ERB view I can cell it like this:

<%= cell(:acquired_skills, wh.acquired_skills).show %>

And the cell renders just fine.

But elsewhere in the code I need to render this cell from a controller (as a result of AJAX call), and I can't figure out the API.

Calling it the same way as a view results in doesn't work - rails doesn't render the result of the cell call and instead looks for a template based on the controller's method name

Calling it like this:

render_cell(:acquired_skills, wh.acquired_skills).show

gives: AbstractController::ActionNotFound (The action '#' could not be found for AcquiredSkillsCell)

Calling it like this:

render_cell(:acquired_skills, :show, @work_history.acquired_skills)

gives: ArgumentError in AcquiredSkillsController#create wrong number of arguments (1 for 0)

Which implies it's passing an argument to the show method... I could define show with an argument, but then I won't get the implicit model instance variable.

Any ideas? I think this would be easier if I could find the API documentation for render_cell :/

Sam

Sam Stickland
  • 637
  • 1
  • 6
  • 17
  • So, I found I can make it work like this: render html: cell(:acquired_skills, @work_history.acquired_skills).show, content_type: 'text/html' I had to explicitly add the content_type otherwise it returned text/javascript and doing so prevented my AJAX form callback from firing, This works, but it doesn't feel particularly DRY. – Sam Stickland Apr 06 '15 at 08:43

1 Answers1

4

You should update to Cells 4. The invocation in controller and view are identical.

html = cell(:comment, @comment).(:show)

It is then up to you how to use that in a controller - the cell doesn't know anything about HTTP, hence you have to call render html: html or something along that.

apotonick
  • 520
  • 2
  • 9
  • Thanks Nick. I ended up calling it like this: render html: cell(:comment, @comment).show.html_safe, content_type: 'text/html' I found both the html_safe and the content_type are needed to make it render correctly from an AJAX callback. – Sam Stickland Jun 14 '15 at 13:28
  • Why the simple html = cell(:comment, @comment).show does NOT work and the strange html = cell(:comment, @comment).(:show) does? What kind of ruby syntax is it anyways? – gorn Jun 18 '17 at 22:31