?
Solved

Web form and populating

Posted on 2007-10-17
2
Medium Priority
?
468 Views
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?

Thx,

0
Comment
Question by:dkim18
2 Comments
 
LVL 24

Accepted Solution

by:
fridom earned 2000 total points
ID: 20101210
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])

  end
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 %>
<p>
I found the user '<%= @user.login_name %>'
<p>
<% else %>
<p>
The user could not be found
</p>
<% 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

Regards
Friedrich
0
 

Author Comment

by:dkim18
ID: 20104717
Great! it works. Easier than I thought...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 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