简体   繁体   中英

changing form_tag to form_for

I want to change this form from form_tag to form_for and thus I will be able to use two submit buttons in the same form.How can I change it ?

gallery.html.haml

- form_tag bulk_content_destroy_admin_project_path(params[@project.id]), :method => "get" do
            -if @entries.length == 0
              %h1 Theres is no design in project.
            -else 
              %input.select_all{:type => "checkbox"}
              = submit_tag "Delete All", :style => "margin-bottom: 10px;" 
              %ul.gallery
                -@entries.each  do |p|
                  %li.gallery_list{ :id => 'entry_' + p.id.to_s }
                    .left_column
                      = check_box_tag "designs_ids[]", p.id, false, :class => "chose_data"
                      %a.group{:href => p.design_url(:original) }
                        =image_tag(p.design_url(:_300px), {:title => p.title, :width => '300px', :height => '225px' }) 
                      .info    
                        %br  
                        ="#" + p.entry_no.to_s +  " - " +  p.title 
                        %ul
                          -if p.withdrawn? 
                            %li 
                              Design is withdrawn
                          %li 
                            Designer :
                            =link_to p.owner.user_name, admin_users_profile_path(p.owner) if p.owner
                          %li
                            Rating :
                            =p.rating
                          %li
                            =link_to "Download File", p.design_url(:original)
                          -if p.view_in_showcase == true
                            =link_to "Remove from top", showcase_admin_project_path(:id => p.project_id, :content_id => p.id)
                          -else  
                            =link_to "Add to top", showcase_admin_project_path(:id => p.project_id, :content_id => p.id)
                          %li
                            = link_to "Delete", content_destroy_admin_project_path(:id => p.project_id, :content_id => p.id, :page => params[:page]) 

form_tag and form_for helpers in rails what you can do with form_for same can be done using form_tag.

You can use single form(form_tag) with two different submit button. Once you submit the form you can check in controller action which submit button was clicked using params[:commit] . Based on this you can perform your task.

For example:

 <%= submit_tag 'Delete All', :name => 'delete_all' %>
 <%= submit_tag 'your other submit', :name => 'other_submit' %>

 if params[:delete_all] #in controller
   #do something
 elsif params[:other_submit]
   #do something
 end

 or

# if you don't want to give name attribute
 commit_action = params[:commit].gsub(' ', '_').downcase
 if commit_action.eql? 'delete_all'
   #do something
 elsif commit_action.eql? 'other_commit'
   #do something
 end 

form_for is used when you want form to know model attributes. Other than that its a normal form. form_tag is a basic form.

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