ruby-on-rails-3.1controllerclonecustomizationhobo

Clone action in hobo


I am writing code to clone object but have no cue from Hobo documentation.

I know how to do it in Rails but it seems no intuitive way to do it in Hobo.

The scenario is to add an button on a record page, clone an existing record, and redirect to new record after the creation.

For example

POST /papers/10/clone

Then the new record 123 will be created.

After that action, it will redirect to page /papers/123

Are there any examples and guides to achieve the scenario?

If Paper has many Comments, how to clone them as well without permission errors?


Solution

  • Hobo doesn't have any special support for cloning an object, so you'll have to drop down to Rails to do it:

    def clone
      paper = Paper.find(params[:id])
      fail if paper.nil?
      clone = paper.clone
      clone.save!
      redirect_to paper_path(clone)
    end
    

    You could add this new controller action to your routes via Hobo's web_method hook, but that's designed for something else. You're better off just adding it to your config/routes.rb in a standard Rails fashion.