How to Pass incoming params with redirect_to?
I'm a neophyte to Ruby On Rails (3). Using Get and Post, I'm trying to create a simple login functionality. After the login credentials are SUBMITed, I've written the following code in my controller,开发者_JS百科
def login @title = "Test Login"
if request.post? and params[:user]
@user = User.new(params[:user])
user = User.find_by_username_and_password(@user.username , @user.password)
if user
session[:user_id] = user.id
flash[:notice] = "User #{user.username} logged in!"
redirect_to :action => "index" , :username => params[:username]
The problem lies here, I want to print the name that was entered in the username textfield. Hence, I'm redirecting it to index along with params. Please help me, get my syntax right, inorder to transfer the username parameter to index with redirect_to.
That should work.
Is the code giving you an error?
Updated from data.
Think I got what is happening. It is how you are trying to display it.
First inside your index method you need a line such as
@username = params[:username]
the @username will make the variable available to your index.html.erb file as it becomes an instance variable for the controller.
In index.html.erb
Welcome Back <%= @usename %>
That should do it.
精彩评论