vendredi 16 septembre 2016

Rails - approve the status before saving to db

I'm building an app that needs to have a check_box in a Devise form, so users can make a request to be a "partner".

Here's what i have in mind:

  • User request to be a partner, checking a check_box in their account update.
  • After user click submit and update their account, i receive an email with the information that the user wants to be a "partner";
  • As an admin, i make the user a "partner"

Here's what i've so far:

controller

def update          
    if params[:partner] == '0'
        UserMailer.new_partner(@user).deliver_now
    end
    if @user.update(user_params)
        flash[:notice] = 'User Updated'
        redirect_to dashboard_path(anchor: 'account')
    else
        flash[:notice] = "You don't have permissions to edit this user"
        render dashboard_path(anchor: 'edit_account')
    end
end
...


private

def user_params
    params.require(:user).permit(:name, :email, :encrypted_password, :password, :partner, :password_confirmation, :admin, :avatar, :bio, :user_id)
end

form

        <%= form_for(@user, :as => current_user, :url => registration_path(:user),  html: { multipart: true }) do |f| %>
      <div class="form-group">
        <%= f.label :avatar %>
        <%= f.file_field :avatar, class: "form-control" %>
      </div>
      <div class="form-group">
        <%= f.label t(:fullname) %>
        <%= f.text_field :name, class: "form-control" %>
      </div>
      <div class="form-group">
        <%= f.label t(:email) %>
        <%= f.text_field :email, required: true, class: "form-control" %>
      </div>
      <% if user_signed_in? and current_user.parceiro? %>
        <div class="form-group">
          <%= f.label t(:about) %>
          <%= f.text_area :bio, rows: "7", class: "form-control" %>
        </div>
      <% end %>
      <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
        <p>Currently waiting confirmation for: <%= resource.unconfirmed_email %></p>
      <% end %>
      <% if !user_signed_in? %>
        <div class="form-group">
          <%= f.label :password %>
          <%= f.password_field :password, autocomplete: "off", hint: "Deixar em branco se não pretender alterar", required: true, class: "form-control" %>
        </div>
        <div class="form-group">
          <%= f.label t(:password_confirmation) %>
          <%= f.password_field :password_confirmation, autocomplete: "off", required: true, class: "form-control" %>
        </div>
      <% else %>
        <div class="form-group">
          <%= f.label t(:current_password) %>
          <%= f.password_field :current_password, autocomplete: "off", required: true, class: "form-control" %>
        </div>
        <% if !current_user.partner? and !current_user.admin? %>
          <div class="form-group">
          <%= f.check_box :partner %>
          <%= f.label t(:want_to_be_partner) %>
          </div>
        <% end %>
      <% end %>
      <div class="form-actions form-group">
        <%= f.submit :submit, class: "btn btn-default" %>
      </div>
    <% end %>

With this code, when user update account, they become partner after clicking update. I need to approve it first.

Any help appreciated. Thanks




Aucun commentaire:

Enregistrer un commentaire