Web form and populating

Posted on 2007-10-17
Last Modified: 2013-11-13
Hi Experts,

I am a rails newbie and I couldnt find good example for the following task.

I have USERS table with login_name column and web form that take login name as parameter and see if login name exists in the USERS table.
If exists, it populate in web form, if not, display appropriate error message.

I dont know how to pass the parameter in controller and populating on the web form if the user name exists.

Can somebody show me a brief example?


Question by:dkim18
    LVL 24

    Accepted Solution

    Here's an example:
    Let us assume yo have a lookup user page:
    This is the code for rendering it:
    <h1>Look up a user</h1>

    <% form_tag :action => 'show_user', :id => @user do %>
        <%= text_field_tag :user_id_to_look_for, params[:user_id_to_look_for]
        <%= submit_tag "Look for" %>

    <% end %>

    The action which will take place is show_user, I implemented it in the controller but in fact it should probably be better placed in the user model)
     def show_user
        # should probably be put into the user model
        @user = User.find_by_id(params[:user_id_to_look_for])

    As you can see the thing I'm looking for is user_id_to_look_for. It was placed in the view and it was placed in the controller. find_by_id has the advantage that it will yield nil if the user can not be bound.

    I stored the value in the @user variable. I now can simply access it in my view:
    <% if(@user) then %>
    I found the user '<%= @user.login_name %>'
    <% else %>
    The user could not be found
    <% end %>

    Now if the user was found I wil print it in ' '
    if the user can not be found we just write out some information, that the requested user can not be found.

    This all and much more is well explained in "Agile Web Developmeng with Rails"

    You should consider lending it and well I just can suggest to buy it.. It's really good


    Author Comment

    Great! it works. Easier than I thought...

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
    Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now