开发者

Form data processing in Ruby on Rails

I am new to Rails and now trying to write a small project in Rails. As part of this project I want to process a form Data(Password). My form is

<form name="input" action="http://0.0.0.0:3000/submit/" method="get">

Password: <input type="text" name="password" />

<input type="submit" value="Login" />

If the entered password is correct I want to display another html page, the original password is not stored in Database, means just want to compare against a character string. I created a controller and view by using 'rails generate controller project'. I think the code for password match should be wr开发者_如何转开发itten in the file 'project_controller.rb' in 'app/controllers' directory. But how to write code for password match and how to map the url in 'routes.rb' file ? If 'get' method changed to 'post' what are the necessary changes required ?


First of all routes.rb are file to map url to a function on controller

the basic example of routing is like here

Foo::Application.routes.draw do
  get '/form'  => 'project#form'
  get '/submit'  => 'project#submit'
  get '/logedin'  => 'project#logedin'
end

there I have 3 function in my project_controller.rb which map to a function you can change it to post by changing the 'get' to 'post'

here is the example code of the project_controller.rb

class ProjectController < ApplicationController
def form
    #will render view/project/form.html.erb
end

def submit
    if params[:password] == 'secret'
        redirect_to '/logedin'
    else
        #you can redirect to form again 
        #or render the form again
        render 'form' #render the view/project/form.html.erb
    end
end

def logedin
    render :text  => 'you are loged in'
end

end

form function will just render the default view which is in app/view/project/form.html.erb

submit is the main logic. you can access the form data using variabel caled params

the code is straight forward. you can learn the details on rails guide http://guides.rubyonrails.org/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜