简体   繁体   中英

Rails duplicating item method renders edit form

I am trying to duplicate active Item records.

On my items_controller:

  def duplicate
      @item = Item.find((params[:id]))
      @item_copy = @item.dup
      render 'new'
  end

I call it from show view with:

<%= link_to "copy", duplicate_item_path(params[:id]) %>

routes:

  resources :items do
      member do
        get 'duplicate'
      end
    end

When I click the link, it takes me to edit @item.

If I change my form to

<%= simple_form_for(@item_copy) do |f| %>

I get expected results, a new form filled with @item attributes.

Do I really need a separate form for duplicating or am I missing something?

You need to define @item correctly in the action:

def duplicate
  @item = Item.find(params[:id]).dup
  render 'new'
end

In this case you can have only one form with <%= simple_form_for(@item) do |f| %> <%= simple_form_for(@item) do |f| %> for both actions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM