Recaptcha with Devise in Rails4 - ruby-on-rails-4

I am using devise as an authentication solution for my rails4 app and want to use recaptcha when user signs up but don't know how to do this.

Try following the directions here: https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise
Get your keys from Google/Recaptcha
Install the Recaptcha gem found
here (note: for Rails 3 do gem 'recaptcha', :require =>
'recaptcha/rails')
Add <%= recaptcha_tags %> on your New
Registration view (you must have generated Devise views) the line
before your submit button.
Create/Update your
RegistrationsController - see the "controllers" section in the
README to understand how to set up your own devise controllers. Remember that "devise" expects you to use flash[:notice] and flash[:alert] and not flash[:error].
Don't forget to update the routes once you do.

Related

Getting same style in another page when click to login

I am having a login page for user generated by devise gem.where I removed the registerable from model for only login not for signup.I have also generated an admin which gives default login credentials for user in seed.rb file.I have done some css work in login page. next I have generated an employee page for further process for users.
Here my doubt is the styling part what I have done in login page is also coming in employee page. I dont want that so I wrote some condition in application.html.erb
<% unless user_signed_in? %>
<%= render 'layouts/heading' %>
<% end %>
The method is defined in application.controller.rb
def user_signed_in? %>
render :layouts => 'application'
end
I tried a lot by changing conditions but it is still displaying or showing errors.
You said that you're using "device gem", but since you're talking about authentication, I assume you mean "devise"?
The user_signed_in? method is a helper which should be provided automatically by devise, and you should be able to use it in your views. You shouldn't need to define it in the application controller.
You probably want to make sure you do "before_filter :authenticate_user!" in the relevant controller (you can use the ":only" option if you only want this to apply for certain methods).
It's difficult to give any more information because you haven't said what errors you are getting. I assume you're getting some kind of syntax error on the definition of the "user_signed_in?" method, since you have an invalid "%>"on the end of the line, which I guess you copied-and-pasted from an ERB file.

Should Show 404 error when user provide invalid url in rails

Suppose my website URL is http://localhost:3000,
But when user manually type http://localhost:3000/orders, he should be redirected to root_path or some other path because this url is only valid after session is created.
and I am getting
NoMethodError in OrdersController#index
undefined method `orders' for nil:NilClass
To make sure user will be redirected to root url you have to use some
before_action (before_filter in Rails 3) to your controller.
For example if you're using Device gem for authentication you have to add to your controller:
before_action :authenticate_user! (more details...)
If you have your own authentication system you have to implement manually something like device's authenticate_user! method to set current_user.
If I correctly understood your issue you should get the idea.

Add clickable links to dashboard of rails_admin gem

Using rails_admin gem, I want to create a clickable link to rails_admin dashboard's left hand side bar.
Path of that link should be according to the rails path. I don't want to add any model for the same.
Please help me in adding the link.
You can add in your rails admin initializer:
config.navigation_static_links = {
'Dashboard' => '/admin' #or whatever you used to mount RailsAdmin in your routes file
}

How to use activemodel to validates date_time_select

I am using rails 4.0
I wonder how to validate date_select with activemodel
let's suppose I have the code as follow
app/models/book.rb
class Book
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :title, :written_on
validates :title, presence: true, allow_blank: false
# How validates :written_on if I use date_select Please look in my view below
end
app/controllers/books.rb
class BooksController < ApplicationController
#...
def new
#book = Book.new
end
def create
#book = Book.new(book_params)
if #book.valid?
#Do something
end
end
#...
end
app/views/books/new.html.erb
<% form_for #book do |f| %>
...
<%= f.date_select :written_on %>
...
<% end %>
I have also try adding
attr_accessor 'written_on(1i)'
to my book model but I got the error invalid attribute name 'written_on(1i)'
Really appreciated for the help here.
Just to clarify, I think you're asking why you're not even able to set the written_on attribute, let alone validate it-- when I used your exact code locally and tried to create a new book, on submit I got undefined method `written_on(1i)=' for Book.
This is because the Book model isn't inheriting from ActiveRecord::Base; you're just including ActiveModel::Model and ActiveModel::Validations. The Rails guide on form helpers says "when Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type."
So I started looking through the Rails source to see where this functionality was implemented, and it's currently in ActiveRecord::AttributeAssignment. There is currently an open pull request that moves this functionality to ActiveModel so that in cases like yours, you'd be able to use it by including ActiveModel::AttributeAssignment.
I'm not sure what you can do until that gets merged in and released. I tried including ActiveRecord::AttributeAssignment and still got the same error, and looking at the pull request, it doesn't seem to be that straightforward. You could fork Rails and apply that pull request, but you'd have to maintain your own Rails for a while until that lands, then get back on a released version.

How do I make a method available inside a partial?

I'm a rails beginner learning rails 4 and I'm trying to learn by doing. I'm making a simple blog that I want some simple user authentication on. I'm trying to learn here, so I don't want to implement Devise, etc. I have a header partial that takes care of my site header and I'm trying to put a link to logout that only shows if a user is logged in. I have a simple session controller that has a new action for the signup form, a create action that sets the current user after matching the email and password and sets session[:user_id] = #current_user.id, and a destroy action that nils out the session. In my application controller I have a method like this
def logged_in?
!session[:user_id].nil?
end
In my _header.html.erb partial, I have
<% if logged_in? %>
(My link)
<% end %>
When I load the page it tells me it can't find the "logged_in?" method. Anyone know why? Thanks.
Methods on controllers are by default not exposed to the views (which your partial is part of).
2 solutions:
Create your logged_in? method as a helper method, for example in an AuthenticationHelper. Doing this you cannot access it from controllers, though.
Expose your controller method to the view using helper_method:
class ApplicationController < ActionController::Base
helper_method :logged_in?
def logged_in?
[...]
end
end
Couldn't you just use:
if #current_user