Reputation: 341
I have an instance variable @user
so in HTML <%=link_to @user %>
gives me the result:
{"id"=>2, "restaurant"=>"sea food", "password"=>"123", "name"=>"a", "email"=>"a", "telephone"=>"123", "created_at"=>"2016-10-09T04:00:24.010Z", "updated_at"=>"2016-10-09T04:00:24.010Z"}
I want to get the value of id, but when I write:<%=link_to @user[:id] %>
it returns me the result :/restaurant/home
, which is the route of my home function inside my restaurant controller and I can't understand why.
This is my controller:
class RestaurantController < ApplicationController
def home
@user = session['loginedUser']
end
def login
end
def checkLogin
@user = User.find_by_restaurant(params[:user][:restaurant])
if @user != nil && @user[:password] == params[:user][:password]
session['loginedUser'] = @user
redirect_to :controller=>'restaurant',:action=>'home'
else
session['loginedUser'] = nil
# redirect_to :controller=>'restaurant',:action=>'login'
end
end
def logout
session['loginedUser'] = nil
redirect_to :controller=>'restaurant',:action=>'home'
end
end
Can anybody help? Thanks a lot.
Upvotes: 0
Views: 1788
Reputation: 2586
You should not save complex objects within your session object. Session data is saved within a cookie by default and many browsers accept only cookies until 4kB. Other issues exists too.
I suggest this change:
def checkLogin
...
session['loginedUser'] = @user.id
...
end
def home
@user = User.find session['loginedUser']
end
Your link to should look like this
<%=link_to id: @user.id %>
. This solution is not realy Rails like. There should be an appropriate helper. You can check your routes with rake routes | grep home
. You will see something like xxx_xxx_home_xxx_restaurant /restaurant/home(.format) restaurant#home
. The first part is the helper name and you can add _path
or _url
. This may look like <%=link_to xxx_xxx_home_xxx_restaurant_path id: @user.id %>
Upvotes: 1