Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Form Helpers in Rails Part-2 (Model Objects)

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 516
    Comment on it

    Hi Friends,
    In my previous blog Form Helpers in Rails Part-1, we have talked about only the basic form helpers. Today we will be discussing forms with models. As we know in MVC, forms are mostly used for creating, updating or editing models. So rails provide a rich flexibility to form helpers to be used with model. When we use form helpers with models, we remove _tag prefix, and thus you don't need to ensure the correct parameter name for each tags. For these helpers, first argument is the name of an instance variable and the second is the name of the attribute. Suppose our instance variable is @blog and its title is "Rails Applications". The text_field, will look like this:

    <%= text_field(:blog, :title) %>
    # = > <input id="blog_title" name="blog[title]" type="text" value="Rails Applications"/>
    


    On form submission the title field will be sent to server within params[:blog][:title]. Now after this, lets discuss about building a form for an object.
    To bind a form to an object form_for helper is used. Suppose if we have a controller blogs and its action new is like this:

    def new
      @blog = Blog.new
    end
    


    The form_for will be used to create its form in app/views/blogs/new.html.erb:

    <%= form_for @blog, url: {action: "create"}, html: {class: "blog_form"} do |f| %>
      <%= f.text_field :title %>
      <%= f.text_area :description, size: "60x8" %>
      <%= f.submit "Create" %>
    <% end %>
    


    Its generated HTML will be:

    <form accept-charset="UTF-8" action="/blogs/create" method="post" class="blog_form">
    <input id="blog_title" name="blog[title]" type="text" />
    <textarea id="blog_description" name="blog[description]" cols="60" rows="8"></textarea>
    <input name="commit" type="submit" value="Create" />
    </form>
    


    Thus by writing the least amount of code, we have built the form and in the create action params[:blog] will be a hash with keys :title and :body. With the help of fields_for helper, similar binding can be created without creating form tag to edit additional model objects with the same form. If you have a Blog model with an associated tag model, you can create a form for creating both:

    <%= form_for @blog, url: {action: "create"} do |blog_form| %>
      <%= blog_form.text_field :title %>
      <%= fields_for @blog.tag_detail do |tag_details_form| %>
        <%= tag_details_form.text_field :tag_name %>
      <% end %>
    <% end %>
    
    # = >
    <form accept-charset="UTF-8" action="/blog/create" class="new_blog" id="new_blog" method="post">
      <input id="blog_title" name="blog[title]" type="text" />
      <input id="tag_detail_tag_name" name="tag_detail[tag_name]" type="text" />
    </form>
    


    Record Identification:
    As we are using RESTful resources, by using record identification, you just need to pass model and everything else will be managed by rails. Best practice is to declare model as resource in routes.

    resources :blogs
    


    And you can simply use model names as:

    ## Creating a new blog
    # long-style:
    form_for(@blog, url: blogs_path)
    # same thing, short-style (record identification gets used):
    form_for(@blog)
     
    ## Editing an existing blog
    # long-style:
    form_for(@blog, url: blog_path(@blog), html: {method: "patch"})
    # short-style:
    form_for(@blog)
    


    Record Identifications have shortcuts for namespaced routes also. Suppose you have developer namespace, form will be submitted to blog controller inside developer namespace:

    form_for [:developer, @blog]
    


    As sometimes we need to use the patch, put or delete requests beside get or post. But few browsers doesn't support them. So rails overcomes this issue by adding another method name inside post as a hidden field named _method.

    form_tag(blog, method: "delete")
    # = > 
    <form accept-charset="UTF-8" action="/delete" method="post">
      <input name="_method" type="hidden" value="delete" />
      <input name="utf8" type="hidden" value="&#x2713;" />
      <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748d7489374aaa2b71" />
      ...
    </form>
    


    Thus we have covered forms for model section and will be covering the next section of form helpers in the next part. Click below to view the next part

    Form Helpers in Rails Part-3

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: