1

Forgive my ignorance but I am quite new to RoR. I am working on a project where users are able to duplicate a post in order to edit this "cloned version" and to save it (with a new post id, of course).

First I tried to use the Amoeba gem described like here, but I failed.

Then I thought I found a better solution - Duplicating a record in Rails 3 - but when I am integrating the suggested code, I am receiving the following error:

NoMethodError in Posts#show undefined method `clone_post_path' for #<#:0x0000010267b8c8>

Researching and tinkering for hours now, I would really appreciate any help!

I am using Rails 3.2.13.

In my posts_controller I have the following code:

  def show
    @post = Post.find(params[:id])

    respond_to do |format|
       format.html # show.html.erb
       format.json { render json: @post }
    end
   end

  def new
    @post = current_user.posts.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  def clone
    @post = current_user.posts.find(params[:id]) # find original object
    @post = current_user.posts.new(@post.attributes) # initialize duplicate (not saved)
    render :new # render same view as "new", but with @post attributes already filled in
  end

  def create
    @post = current_user.posts.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

This is the post.rb model:

 class Post < ActiveRecord::Base
    attr_accessible :content, :title, :videos, :link, :description
    validates :title, presence: true
    belongs_to :user
 end

In the show view I call this:

<%= link_to 'Create a clone', clone_post_path(@post) %>

What am I doing wrong? Thank you so much in advance for any help!

UPDATE: Adding

resources :posts do
   get 'clone', on: :member
   end

to the routes file worked.

Here is the routes file:

 Tt::Application.routes.draw do

   devise_for :users
   get 'about' => 'pages#about'
   resources :posts
   root :to => 'pages#home'
   post 'attachments' => 'images#create'

   resources :posts do
   get 'clone', on: :member
   end

end

Unfortunately afterwards a new error occurred:

ActiveModel::MassAssignmentSecurity::Error in PostsController#clone Can't mass-assign protected attributes: id, created_at, updated_at, image_file_name, image_content_type, image_file_size, image_updated_at, file, user_id

Community
  • 1
  • 1
YvonC
  • 329
  • 2
  • 6
  • 22
  • Can you please also post the routes pertaining to Post controller? I am guessing that you did not add get :clone, on: :member to your post routes. – davidfurber Nov 15 '13 at 16:19
  • Thx David! See above. – YvonC Nov 15 '13 at 16:50
  • @davidfurber Your hint with "on: member" worked! See above. Made my day! Unfortunately now I have a new error: ActiveModel::MassAssignmentSecurity::Error in PostsController#clone Can't mass-assign protected attributes: id, created_at, updated_at, image_file_name, image_content_type, image_file_size, image_updated_at, file, user_id – YvonC Nov 15 '13 at 16:56
  • See this post for your mass-assign problem, it might be able to help. http://stackoverflow.com/questions/10574957/activemodelmassassignmentsecurityerror-cant-mass-assign-protected-attribut – sabrams Nov 15 '13 at 17:02

1 Answers1

0

Make sure that your routes file includes the following:

resources :posts do
  get 'clone', on: :member
end

Since the clone action is not a standard action you must account for it in your routes file so it knows what to do.

sabrams
  • 1,128
  • 2
  • 15
  • 28
  • Thank you so much for the answer! I added your suggestion, but the error is still there. I included my routes file above. – YvonC Nov 15 '13 at 16:44
  • I realized that davidfurber had it right up there, it's on: :member instead of :collection. – sabrams Nov 15 '13 at 17:00