将form_tag更改为form_for

时间:2013-01-29 14:58:51

标签: ruby-on-rails form-for

我想将此表单从form_tag更改为form_for,因此我可以在同一表单中使用两个提交按钮。如何更改?

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]) 

1 个答案:

答案 0 :(得分:1)

可以使用form_tag在rails中使用

form_tag form_for 帮助程序完成相同的操作。

您可以使用单个表单(form_tag)和两个不同的提交按钮。提交表单后,您可以使用 params [:commit] 检查控制器操作,其中单击了提交按钮。基于此,您可以执行任务。

例如:

 <%= 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 。除此之外,它是一个正常的形式。 form_tag是一种基本形式。