Discussion Forums
- Topic List
- Most Recent Posts
- Sign In for more options
Good Day Wishes .
I am the beginner for RoR. I tried a lot but unable to capture the whole of the “FORM†and its usage. Kindly give me a step-by-step procedure to learn how forms work?
suggest a sample example ?
Thank you in advance.
Hello There,
I'll try to explain how forms work in a few steps, so here we go:
- to start you need to put a form_for tag in your view (for example we're going to make a blog app)
So piece by piece - form_for -> creates an html form tag - @post is the collection where all your data will be put in - do |p| -> that we will see later - end -> we need to close the form tag
Between the form tags you can place for example a text_field like this :
- p -> is an instance of the db field
- submit -> this creates a submit button for submitting your data
Your controller could look like this :
def new
@post = Post.new
end -> here you create an instance that you'll use in your form def create
@post = Post.new(params[:post]) -> We create a new post entry with all the data of our form
if @post.save and request.post? -> "security" check
flash[:notice] = "This post is created"
redirect_to posts_path
else
render :action => 'new' -> else show the form again
end
end
If you're still having question, just fire away:)
