Search code examples
ruby-on-railsrails-cells

Rendering a rails cell from a controller with an implicit model


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


Solution

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