I am building an application that requires a C++ program to access the Rails web application using POST. Now, I just added user authentication to the mix and it has made things more interesting. I''m using basic authentication as shown in the AWDWR and Rails Recipes books. I can now get my C++ program to submit the user credentials to the login/login page. All I want the login page to do (in this case) is to: 1. Set the session cookie and return it to me (so that I can pass it up when I submit the data to the main form) 2. Indicate a success code - Success/ Failed. The login action is also used by other parts of the web application which requires it to redirect to: 1. Either the action saved in the session 2. the Administration index page My C++ Client has a custom user_agent setting so that I know when the request has come from it. Now, I''m trying to figure out how to return a specific value to this client only - hence, the need to render nothing (special) and go to no specific URL. Any ideas? This is what my login/login action looks like. def login session[:user_id] = nil if request.post? user = User.authenticate(params[:name], params[:password]) if user session[:user_id] = user.id if session[:intended_action].nil? || session[:intended_controller].nil? if (@request.env[''HTTP_USER_AGENT''] == "MyCustomCClient") #did it come from my C++ program? #### <---- I need something here -----> #### else redirect_to(:action => "index") #deals with direct access to /login/login end else redirect_to :action => session[:intended_action], :controller => session[:intended_controller] end else flash[:notice] = "Invalid user/password combination" end end end Thanks, Mohit.
I think I may have figured this out by reading the manual - apologies for the question. It seems that I could use: # Renders the clear text "hello world" with status code 200 render :text => "hello world!" Cheers Mohit. Mohit Sindhwani wrote:> I am building an application that requires a C++ program to access the > Rails web application using POST. Now, I just added user > authentication to the mix and it has made things more interesting. > > I''m using basic authentication as shown in the AWDWR and Rails Recipes > books. I can now get my C++ program to submit the user credentials to > the login/login page. All I want the login page to do (in this case) > is to: > 1. Set the session cookie and return it to me (so that I can pass it > up when I submit the data to the main form) > 2. Indicate a success code - Success/ Failed. > > The login action is also used by other parts of the web application > which requires it to redirect to: > 1. Either the action saved in the session > 2. the Administration index page > > My C++ Client has a custom user_agent setting so that I know when the > request has come from it. Now, I''m trying to figure out how to return > a specific value to this client only - hence, the need to render > nothing (special) and go to no specific URL. Any ideas? This is what > my login/login action looks like. > > def login > session[:user_id] = nil > if request.post? > user = User.authenticate(params[:name], params[:password]) > if user > session[:user_id] = user.id > if session[:intended_action].nil? || > session[:intended_controller].nil? > if (@request.env[''HTTP_USER_AGENT''] == "MyCustomCClient") > #did it come from my C++ program? > #### <---- I need something here -----> #### > else > redirect_to(:action => "index") #deals with direct > access to /login/login > end > else > redirect_to :action => session[:intended_action], > :controller => session[:intended_controller] > end > else > flash[:notice] = "Invalid user/password combination" > end > end > end > > Thanks, > Mohit.
You could also use : render :nothing => true That will just send a 200 ok response with no content -Ezra On Aug 13, 2006, at 10:54 AM, Mohit Sindhwani wrote:> I think I may have figured this out by reading the manual - > apologies for the question. > It seems that I could use: > > # Renders the clear text "hello world" with status code 200 > render :text => "hello world!" > > Cheers > Mohit. > > > > > Mohit Sindhwani wrote: >> I am building an application that requires a C++ program to access >> the Rails web application using POST. Now, I just added user >> authentication to the mix and it has made things more interesting. >> >> I''m using basic authentication as shown in the AWDWR and Rails >> Recipes books. I can now get my C++ program to submit the user >> credentials to the login/login page. All I want the login page to >> do (in this case) is to: >> 1. Set the session cookie and return it to me (so that I can pass >> it up when I submit the data to the main form) >> 2. Indicate a success code - Success/ Failed. >> >> The login action is also used by other parts of the web >> application which requires it to redirect to: >> 1. Either the action saved in the session >> 2. the Administration index page >> >> My C++ Client has a custom user_agent setting so that I know when >> the request has come from it. Now, I''m trying to figure out how >> to return a specific value to this client only - hence, the need >> to render nothing (special) and go to no specific URL. Any >> ideas? This is what my login/login action looks like. >> >> def login >> session[:user_id] = nil >> if request.post? >> user = User.authenticate(params[:name], params[:password]) >> if user >> session[:user_id] = user.id >> if session[:intended_action].nil? || session >> [:intended_controller].nil? >> if (@request.env[''HTTP_USER_AGENT''] == >> "MyCustomCClient") #did it come from my C++ program? >> #### <---- I need something here -----> #### >> else >> redirect_to(:action => "index") #deals with direct >> access to /login/login >> end >> else >> redirect_to :action => session >> [:intended_action], :controller => session[:intended_controller] >> end >> else >> flash[:notice] = "Invalid user/password combination" >> end >> end >> end >> >> Thanks, >> Mohit. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks, Ezra! In retrospect, I''d like to render a bit of text so that my application receives the status directly.. but your solution is perfect (and actually what I had asked for)! Cheers Mohit. Ezra Zygmuntowicz wrote:> You could also use : > > render :nothing => true > > That will just send a 200 ok response with no content > > -Ezra > > > > On Aug 13, 2006, at 10:54 AM, Mohit Sindhwani wrote: > >> I think I may have figured this out by reading the manual - apologies >> for the question. >> It seems that I could use: >> >> # Renders the clear text "hello world" with status code 200 >> render :text => "hello world!" >> >> Cheers >> Mohit. >> >> >> >> >> Mohit Sindhwani wrote: >>> I am building an application that requires a C++ program to access >>> the Rails web application using POST. Now, I just added user >>> authentication to the mix and it has made things more interesting. >>> >>> I''m using basic authentication as shown in the AWDWR and Rails >>> Recipes books. I can now get my C++ program to submit the user >>> credentials to the login/login page. All I want the login page to >>> do (in this case) is to: >>> 1. Set the session cookie and return it to me (so that I can pass it >>> up when I submit the data to the main form) >>> 2. Indicate a success code - Success/ Failed. >>> >>> The login action is also used by other parts of the web application >>> which requires it to redirect to: >>> 1. Either the action saved in the session >>> 2. the Administration index page >>> >>> My C++ Client has a custom user_agent setting so that I know when >>> the request has come from it. Now, I''m trying to figure out how to >>> return a specific value to this client only - hence, the need to >>> render nothing (special) and go to no specific URL. Any ideas? >>> This is what my login/login action looks like. >>> >>> def login >>> session[:user_id] = nil >>> if request.post? >>> user = User.authenticate(params[:name], params[:password]) >>> if user >>> session[:user_id] = user.id >>> if session[:intended_action].nil? || >>> session[:intended_controller].nil? >>> if (@request.env[''HTTP_USER_AGENT''] == >>> "MyCustomCClient") #did it come from my C++ program? >>> #### <---- I need something here -----> #### >>> else >>> redirect_to(:action => "index") #deals with direct >>> access to /login/login >>> end >>> else >>> redirect_to :action => session[:intended_action], >>> :controller => session[:intended_controller] >>> end >>> else >>> flash[:notice] = "Invalid user/password combination" >>> end >>> end >>> end >>> >>> Thanks, >>> Mohit. >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > >