dimanche 31 décembre 2017

How to create multiple login pages with Devise

I'm using Devise to authenticate. However, I have 3 profile types, and I created the rails g devise MODEL command for my users (Admim, dev and manager), but Devise created a login page for each profile type, I would like to unify and log in using only one page!.

My idea is to create a view by picking up the email and the password and passing it to a custom controller inheriting from ** Devise :: Sessions ** and overwriting the method that logs in, searching the email in the 3 tables and redirecting the user to the Controller responsible for his profile.

How could I do this "manual login" with Devise? Any other solution to this?

samedi 30 décembre 2017

access all events from clockwork manager to controller

Am using gem clockwork for running scheduled jobs. I need to access all events defined in clock.rb for manually trigger the clockwork events when it fails.

For that I forked the clockwork gem by adding attr_reader: events in clockwork/manager.rb file.

clock.rb

module Clockwork
  every(10.seconds, 'job1') do
    p Clockwork.manager.events
  end
end

By using Clockwork.manager.events in clock.rb it returns all events that defined in clock.rb.

mycontroller.rb

module Admin
  class MyController < AdminController

    require 'clockwork'

    def index
      @events = Clockwork.manager.events
    end
  end
end

But while using it in controller it returns empty value.

How to get all clockwork events in controller?

vendredi 29 décembre 2017

Rails 5 - How to get serializable_hash for activerecord?

In rails 5, I am trying to get a data in serializable_hash format. First set of data format is in correct but second set of data is in different format. The Comment module works like parent and children format, one comment has_many comments by adding parent_comment_id.

For parent comment, now data is {id: 77, name: "l1", verified: true, category: "Movie", slug: "l1", …}

For child comment, data is coming like {id: 78, name: "l2", category_id: 61, …}

Here I need to get second set of data for comments as same as first format. I have tried using serializable_hash but it supports only single row. How can I achieve this for all comments(has_many association)?

Error when delete comment Ruby on Rails

When I delete comment I see

undefined method 'each' for nil:NilClass

Also, the user cannot delete his account if has posted... It makes the error.

This is code

<p id="notice"><%= notice %></p>

<h1>Comments</h1>

<table>
  <thead>
    <tr>
      <th>Link</th>
      <th>Body</th>
      <th>User</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @comments.each do |comment| %>
      <tr>
        <td><%= comment.link_id %></td>
        <td><%= comment.body %></td>
        <td><%= comment.user %></td>
        <td><%= link_to 'Show', comment %></td>
        <td><%= link_to 'Edit', edit_comment_path(comment) %></td>
        <td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Comment', new_comment_path %>

Ruby odd number of arguments for hash error

case look
  when 'side'
        hash1 = {
            'num' => 5,
            'style' => 'sideStyle',
        }

  when 'front'
        hash1 = {
            'num' => 8,
            'style' => 'frontStyle',
        }

  when 'back'
        hash1 = {
            'num' => 4,
            'style' => 'backStyle',
            'special' => 'yes',
        }

  else
        hash1 = {
            'num' => 2,
            'style' => 'noStyle',
        }
  end

  myStyle = Hash[hash1]

My piece of code looks like this.

when I run this code I get "odd number of arguments for Hash".

is this the correct way to from an hash? could someone please help me how to get it resolved.

Sorting Records on a Custom calculated value at the runtime- rails

I have Two models Product, Price

Product 
 has_many :prices

Price
 belongs_to :product

we have price_type and price_date attributes in the Price Model. price_type can be default, weekend, specific_date for a particular Product.

Product can have single price on a day like either it is weekend, or a specific_date otherwise default.

price_date will be matter when price_type will be specific_date

Now The problem is,

Date.today is passing in the parameters

I want to get all the products in the sorted by price on that date which is passing.

I'm trying to find the solution since a long time!

Please Help Me!

Thanks

Translate error messages using rails (I18n) Api

I am using rails internationalization api for activerecord translations .I am having trouble translating error messages. How to translate multiple error messages from different files in my project?I am have files named application_draft.rb,team.rb,user.rb,todo.rb inside my models folder.I want to translate error messages in them ,here's my en.yml file:

errors:
        models:
         -team:
         -application_draft:
         -conference:
         -todo:
         -user:
            attributes:
              roles:
                too_many_reviewers: too many reviewers
                multiple_sel_error: must not be selected twice
                must_accepted: must have been accepted
                one_app_allowed: Only one application may be lodged
                confirmed_email_address: Please make sure every student confirmed the email address.
                wrong_date_sel: must be a later date than start date
                no_more_than_two: "there cannot be more than 2 students on a team."
                cannot_changed: can't be changed

I have implemented this code and throws error(means it did not work). Here's my one of application_draft.rb and todo.rb error code snippets:

application.rb:

def different_projects_required
  if project1 && project1 == project2
    errors.add(:projects, :multiple_sel_error)
  end
end

todo.rb

def validate_number_of_reviewers
  errors.add(:user, :too_many_reviewers) if application.todos.count > 3
end
end

How to translate these both avoiding duplication errors?