Hi Sergio

*Model tasks.rb*
def initialize_dup(other)
    super
    self.items = other.items.map(&:dup)
  end

*tasks_controller.rb*

# 14/3/17
   conf.actions << :duplicate
   conf.duplicate.link.method = :get
   conf.duplicate.link.position = :after
   conf.duplicate.link.page = true # for new page rendering
   # 14/3/17

Issue : When click the dup link , get

Started POST "/tasks/27/duplicate?_method=post" for ::1 at 2017-03-14 
13:09:45 +
0800
Processing by TasksController#duplicate as JS
  Parameters: {"id"=>"27"}
  Task Load (0.0ms)  SELECT  `tasks`.* FROM `tasks` WHERE `tasks`.`id` = 27 
LIMI
T 1
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.0ms)


*NoMethodError (undefined method `items' for #<Task:0xa3c6d58>):*

app/models/task.rb:28:in `initialize_dup'
  Rendering 
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.
1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb
  Rendering 
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.
1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb
  Rendered 
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.1
/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (15.6ms)
  Rendering 
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.
1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
  Rendered 
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.1
/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
  Rendering 
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.
1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.er
b
  Rendered 
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.1
/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
 (0.0ms)
  Rendered 
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.1
/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb 
(1160.2ms

Q. Where have I gone wrong ?
Ref: https://github.com/activescaffold/active_scaffold_duplicate


On Tuesday, 14 March 2017 12:42:46 UTC+8, Chee Chong Hwa wrote:
>
> Hi Segio
>
> This is exactly what I want to do :-)
>
> Sergio:
> Anyway, you can use active_scaffold_duplicate gem, it adds :duplicate 
> action, 
> which can be setup to create new record on click, or 
> *open a form prefilled with cloned record.* If you have uniqueness 
> validation, you need to generate new 
> unique value for column or open form instead of duplicating on click. 
>
> Gonna try it out now :-)
>
> On Monday, 13 March 2017 19:42:57 UTC+8, Sergio Cambra wrote:
>>
>> El sábado, 11 de marzo de 2017 4:29:10 (CET) Chee Chong Hwa escribió: 
>> > HI Sergio 
>> > 
>> > Trace from rails server 
>> > Started GET "/tasks/26/clone?adapter=_list_inline_adapter" for ::1 at 
>> > 2017-03-11 
>> >  11:26:59 +0800 
>> >   ActiveRecord::SchemaMigration Load (0.0ms)  SELECT 
>> `schema_migrations`.* 
>> > FROM 
>> > `schema_migrations` 
>> > Processing by TasksController#clone as JS 
>> >   Parameters: {"adapter"=>"_list_inline_adapter", "id"=>"26"} 
>> >   Task Load (15.6ms)  SELECT  `tasks`.* FROM `tasks` WHERE `tasks`.`id` 
>> = 
>> > 26 LIM 
>> > IT 1 
>> >    (0.0ms)  BEGIN 
>> >   Task Exists (0.0ms)  SELECT  1 AS one FROM `tasks` WHERE 
>> `tasks`.`task` = 
>> > BINA 
>> > RY 'Activate Authentication' LIMIT 1 
>> >    (0.0ms)  ROLLBACK 
>> > No template found for TasksController#clone, rendering head :no_content 
>> > Completed 204 No Content in 10278ms (ActiveRecord: 15.6ms) 
>>
>> Task is loaded, then there is a query looking for tasks.task = 'Activate 
>> Authentication'. I guess you have some uniqueness validation on task 
>> column. 
>>
>> Also, you have no view for clone action, you should add one to render new 
>> task, or display errors, for example using flash. Or you can redirect to 
>> index 
>> action. You should check if save worked, if failed then you can check 
>> new_task.errors for validation errors: 
>>
>> if new_task.save 
>>   render or redirect 
>> else 
>>   flash.now[:error] = ... 
>>   render ... 
>> end 
>>
>> If you are going to redirect when saving new task fails, you should use 
>> flash[:error] instead of flash.now 
>>
>> Another thing, you shouldn't use GET requests for actions which save to 
>> database, I would use PUT (change route to put and add :method => :put to 
>> action_link). If action renders a js view, add :position => false to 
>> action_link, default :position is :replace which expects action to return 
>> html 
>> to display. 
>>
>> Anyway, you can use active_scaffold_duplicate gem, it adds :duplicate 
>> action, 
>> which can be setup to create new record on click, or open a form 
>> prefilled with 
>> cloned record. If you have uniqueness validation, you need to generate 
>> new 
>> unique value for column or open form instead of duplicating on click. 
>>
>> > 
>> > On Saturday, 11 March 2017 11:24:45 UTC+8, Chee Chong Hwa wrote: 
>> > > Hi Segio 
>> > > 
>> > > Tasks Controller 
>> > > 
>> > >  conf.action_links.add 'clone', :label => 'Clone', :page => false, 
>> :type 
>> > > 
>> > > => :member 
>> > > Nb. This displays corretly on per record basis 
>> > > 
>> > > def clone 
>> > > 
>> > >  old_task = Task.find(params[:id]) 
>> > >  new_task = old_task.dup 
>> > >  new_task.save 
>> > > 
>> > > end 
>> > > 
>> > > Routes.rb 
>> > > 
>> > >   resources :tasks, concerns: :active_scaffold 
>> > > 
>> > > resources :tasks do 
>> > > 
>> > >     member do 
>> > >     
>> > >      get 'clone' =>"tasks#clone" 
>> > >     
>> > >     end 
>> > >     as_routes 
>> > >   
>> > >   end 
>> > > 
>> > > Issue: When I click clone... something is happening but the duplicate 
>> > > record is not being save or perhaps not duplicated at all 
>> > > 
>> > > BTW, I got the idea from 
>> > > 
>> http://stackoverflow.com/questions/60033/what-is-the-easiest-way-to-duplic 
>> > > ate-an-activerecord-record 
>> > > 
>> > > Q. What have I done wrong ? 
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails Gem" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/activescaffold.
For more options, visit https://groups.google.com/d/optout.

Reply via email to