I'm a newbie for jRuby/Ruby on Rails and trying to create a website (Learning purpose).
In this web application users need to register to the system. Upon successful login attempt they are allowed to maintain their own dashboard which is a simple phone contact details management.
I'm using
jruby 9.0.5.0 (2.2.3)
Rails 4.2.6
Windows 10
MySQL
in XAMPP
installationQuestion no 1
In my signup, when I try to insert a new user, function works but I'm not getting the values in my textfields. Instead I'm getting null values in my table. What am I doing wrong?
This is my create User
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password
t.timestamps null: false
end
end
end
This is my user.rb
model
class User < ActiveRecord::Base
begin
attr_accessor :name, :email, :password, :password_confirmation
end
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true
validates :password, :confirmation => true #password_confirmation attr
validates_length_of :password, :in => 3..20, :on => :create
end
This is my users_controller.rb
class UsersController < ApplicationController
def login
end
def create
flash[:notice] = ''
@user=User.new(user_params)
if @user.valid?
@user.save
flash[:notice] = "You signed up successfully"
render 'users/login'
else
flash[:error] = "Form is invalid"
render 'users/signup'
end
end
def signup
@user = User.new
flash[:notice]=''
flash[:error]=''
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
This is my signup.html.erb
<div class = "content">
<h1>Sign Up</h1>
<p>Maecenas nisi nunc, vulputate ac tempus eget, suscipit ut sapien. Aenean fringilla ultricies ultricies.
Mauris aliquet nunc in velit posuere, in convallis purus ullamcorper.
Nam imperdiet lacus lacus, quis finibus diam dapibus a. Nullam quis accumsan libero. </p>
<div class = "form_data">
<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %>
<table>
<tbody>
<tr>
<td> Name : </td>
<td> <%= f.text_field :name %> </td>
</tr>
<tr>
<td> Email : </td>
<td> <%= f.text_field :email %> </td>
</tr>
<tr>
<td> Password : </td>
<td> <%= f.password_field :password %> </td>
</tr>
<tr>
<td> Re-type Password : </td>
<td> <%= f.password_field :password_confirmation %> </td>
</tr>
<tr>
<td> </td>
<td> <%= f.submit :signup %> </td>
</tr>
</tbody>
</table>
<% end %>
</div>
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
</div>
Question no 2
How to create a contact_details
table (id, name, phonenumber, owner_id)
with user_ID
of the user table as the foreign key (owner_id)
.
Question no 3
How to create the login algorithm nice and neat. Any links to a tutorial is appreciated
Question no 4
How to edit the Select queries in Jruby/Ruby on rails
so that I can get the details based on the logged user.
Answer 1
When you use attr_accessor
it tells the model that you don't want these attributes to be saved in the database, but just used as model properties in the application. Remove the block below and the data should save fine.
begin
attr_accessor :name, :email, :password, :password_confirmation
end
Answer 2
The best place to read on this is the Rails Guides, Active Record Associations. Explanation hardly gets better than that.
Here's a gist of what you need to do without spoon feeding is:
You will need to generate a UserDetail
model/scaffold with the parameters. id, name, phonenumber, user_id
(note the use of user_id. It is a rails convention for foreign key).
Your model declaration will then look like,
class User < ActiveRecord::Base
has_one :user_detail
end
class UserDetail < ActiveRecord::Base
belongs_to :user
end
Answer 3
Here's the best ever video on how to create authentication from scratch by Ryan Bates. http://railscasts.com/episodes/250-authentication-from-scratch-revised
Answer 4
The video in Answer 3 will answer this question as well, but basically when you have that authentication setup, you can get the details simply by doing
current_user.user_detail
Update
Just realized that the video link I posted is a pro video and requires subscription. If you already have one, and I strongly recommend it if you are learning rails, you should be able to see it. If not, there are plenty of Authentication from scratch tutorials out there, most based on that video. Following any will do. Also FYI, That site is currently just charging for one month's subscription, and all following months are being renewed free for now (as of writing this.). So its a really good deal, for high quality learning material.