0

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.

1 Answers1

1

I think that you might be solving this issue at the wrong level. Sometimes the best solution is to avoid the difficult problem all together.

What if you where to only have one controller method (name suggestion: expedite_users) and one parameter sent from the form would determine if it was accepting or rejecting. Seems to me that much of the code in both your methods where very similar any way.

Here is a question about having multiple buttons in a single form: How do I create multiple submit buttons for the same form in Rails?

Community
  • 1
  • 1
Albin
  • 2,912
  • 1
  • 21
  • 31