dimanche 13 septembre 2015

How to set two different actions for single form in Ruby on rails

In my application , I have one form in view which shows registered examinees with check boxes. Admin has the authority to confirm and reject the examinees. In my controller i have two methods , one for approve and another for reject. But i am not able to define correct method (action) in form. Because I want approve method to approve the examinees and reject for rejection.I am new to ruby on rails . Please Help me how to resolve this problem. I am pasting related source below.

welcome_controller.rb

class WelcomeController < ApplicationController filter_access_to :all #layout "home" layout :choose_layout

  def confirm_registration
    @all_approved_users = params[:check_examinee]
    @all_approved_users.each do|approved|
      @user = User.find_by_id(approved.to_i)
      @user.update_attributes(:is_approved => 1)
      if (@user.is_approved == 0 and @user.confirmed == false) or (@user.is_approved == 1 and @user.confirmed == false) or (@user.is_approved == 2 and @user.confirmed == false)
       UserMailer.user_registration_email_confirmation(@user,$pwd).deliver
      end
    end
     flash[:success] = t('flash_success.email_verification')
      respond_to do |format|
        format.html { redirect_to :back }
         format.js
      end
  end

  def reject_registration
    @rejected_users = params[:check_examinee]
    @rejected_users.each do|rejected|
      @user = User.find_by_id(rejected.to_i)
      unless (@user.is_approved == 2 and @user.confirmed == false)
      @user.update_attributes(:confirmed => false,:is_approved => 2)
      UserMailer.examinee_registration_rejection(@user).deliver
      end
    end
     flash[:success] = t('flash_success.email_rejection')
      respond_to do |format|
        format.html { redirect_to :back }
         format.js
      end

  end

end

activation.html.erb

<%unless @registered_examinees.empty?%>
    <% form_tag :action => '           ', :method => :post, :class => 'confirm_reject',:id => "class_form" do %>
        <div class="questionTabele">
            <table border="0" cellspacing="0">
                <tr class="updateHeader">
                    <th><%= t('general.name')%></th>
                    <th><%= t('general.email')%></th>
                    <th><%= t('general.status')%></th>
                    <th> <%unless @unapproved_examinees.blank?%>
                    <%= check_box "checkall", "checkall", :title => "Select all unconfirmed users", :onclick => "checkUncheckAll(this,check_examinee_);" %>
                    <%else%>
                    <%= check_box "checkall", "checkall", :disabled => true, :title => "Select all unconfirmed users" %>
                    <%end%> </th>
                </tr>

                <% @registered_examinees.each do|examinee| %>
                <tr class="updateAltrRow <%= cycle("odd", "even") %>">
                    <td><%= examinee.name%></td>
                    <td><%= examinee.email%></td>
                    <td> <%if examinee.confirmed == true%>
                    <%= t('user.confirmed')%>
                    <%elsif examinee.confirmed == false and examinee.is_approved == 0%>
                    <%= t('user.approve')%>
                    <%elsif examinee.confirmed == false and examinee.is_approved == 2%>
                    <%= t('user.reject')%>
                    <%else%>
                    <%= t('user.pending')%>
                    <%end%> </td>
                    <td> <%unless examinee.confirmed == true%>
                    <%= check_box_tag "check_examinee[]", examinee.id, false, {:class=>"validate[minCheckbox[1]] checkbox"} %>
                    <%end%> </td>

                </tr>

                <%end%>

            </table>
        </div>
        <%= submit_tag t('general.approve'), :id=>"confirm", :class=>'btnBg' %>  <%= submit_tag t('general.reject'), :id=>"reject", :class=>'btnBg' %>
        <%= will_paginate @registered_examinees,
        :prev_label => t('general.previous'),
        :next_label => t('general.next'),
        :page_links =>true,
        :renderer => PaginationListLinkRenderer
        %>
    <% end %>
<%else%>
    <div class="formContainer">
        <%= t('not_found.no_examinee_found')%>
    </div>
<% end %>

Need to change anything in routes.rb. Please tell me still I am learning ruby on rails.Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire