Search code examples
ruby-on-railsruby-on-rails-3scaffolding

Is that possible to add destroy action into _form.html.erb instead of index.html.erb


If I do scaffold, it automatically makes destroy action in index.html.erb.
What if I want to move this action to _form.html.erb?
And I'd like to make it appear only during edit mode(example.com/books/1212/edit)
not during new mode(example.com/books/new)


Solution

  • I think this would help you

    model

    class Job < ActiveRecord::Base
      attr_accessible :delete_flag #with other attributes
      attr_accessor :delete_flag
    end
    

    *in your view (_form)*

    <%= form_for(@job) do |f| %>
      #other code
      <%= f.text_field :delete_flag%>
      <div class="actions">
        <%= f.submit %>
        <%= f.submit "Delete",{:class => "delete_button"} %>
      </div>
    <% end %>
    

    coffeescript

    jQuery ->
      $('#new_job').submit -> 
        #capture your delete button click
        $('#job_delete_flag').val("1")
    

    in your controller you will get params as :delete_flag, and from there you could

     Parameters: {"utf8"=>"✓", "authenticity_token"=>"5oWQU+w0jVCQlw8wLvCyKajbBSKpK2sv6RMkSGTE2H8=", "job"=>{"delete_flag"=>"1"}, "commit"=>"Delete"}
    

    HTH