Note that there are some explanatory texts on larger screens.

plurals
  1. POI am trying to duplicate an activerecord record, but receive NoMethodError - undefined method
    text
    copied!<p>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).</p> <p>First I tried to use the <a href="https://github.com/rocksolidwebdesign/amoeba#readme" rel="nofollow noreferrer">Amoeba gem</a> described like <a href="https://stackoverflow.com/questions/60033/what-is-the-easiest-way-to-duplicate-an-activerecord-record">here</a>, but I failed. </p> <p>Then I thought I found a better solution - <a href="https://stackoverflow.com/questions/11993012/duplicating-a-record-in-rails-3?lq=1">Duplicating a record in Rails 3</a> - but when I am integrating the suggested code, I am receiving the following error: </p> <p>NoMethodError in Posts#show undefined method `clone_post_path' for #&lt;#:0x0000010267b8c8></p> <p>Researching and tinkering for hours now, I would really appreciate any help!</p> <p>I am using Rails 3.2.13.</p> <p>In my posts_controller I have the following code:</p> <pre><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 </code></pre> <p>This is the post.rb model:</p> <pre><code> class Post &lt; ActiveRecord::Base attr_accessible :content, :title, :videos, :link, :description validates :title, presence: true belongs_to :user end </code></pre> <p>In the show view I call this:</p> <pre><code>&lt;%= link_to 'Create a clone', clone_post_path(@post) %&gt; </code></pre> <p>What am I doing wrong? Thank you so much in advance for any help!</p> <p>UPDATE: Adding </p> <pre><code>resources :posts do get 'clone', on: :member end </code></pre> <p>to the routes file worked.</p> <p>Here is the routes file:</p> <pre><code> Tt::Application.routes.draw do devise_for :users get 'about' =&gt; 'pages#about' resources :posts root :to =&gt; 'pages#home' post 'attachments' =&gt; 'images#create' resources :posts do get 'clone', on: :member end </code></pre> <p>end</p> <p>Unfortunately afterwards a new error occurred:</p> <p>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</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload