How get the value user id?

Hi, all!

I’m new to RoR.
Please help me. :slight_smile:

======================================================
[ /controller/login.rb ]

def find_id
if request.post?
member = params[:member]
@is_member = Member.find(:first, :conditions => [‘name=? and
email=?’, member[:name], member[:email]])
if @is_member
redirect_to :action => :find_id_done
else
flash[:notice] = “You are NOT member.”
end
end
end


[ /view/login/find_id_done.rhtml ]

  1. Your ID is <%= @is_member.uid %>.
  2. Your ID is <%= is_member.uid %>
  3. Your ID is <%= is_member[:uid] %>

(* the field name of user id is “uid”.)

What is correct?

@{value_name} is instant value ( = status of object)
{value_name} is normal value.
{value_name}[:key] is hash…

Please, Help Me!
Thanks a lot!

Welcome to the cool club :slight_smile:

first, lets start with the controller.

def find_id
if request.post?
@is_member = Member.find_by_name_and_email(params[:member][:name],
params[:member][:email])
if @is_member
redirect_to(:action => “find_id_done”, :member_id => @is_member)
else
flash[:notice] = “You are NOT member.”
end
end
end

def find_id_done
@member = Member.find(params[:member_id])
end

now for the view

Your ID is <%= @member.id %>.
or if you actually have a column called uid, then
Your ID is <%= @member.uid %>

just remmber that when a model is created, and the migration is ran,
that a column called id is automatically created, so you can use that,
unless the you want to specify an id.

~Jeremy

On Oct 17, 4:57 pm, mmnmm7 [email protected] wrote:

  member = params[:member]

What is correct?

@{value_name} is instant value ( = status of object)
{value_name} is normal value.
{value_name}[:key] is hash…

Please, Help Me!
Thanks a lot!

Hi. I am also new, but I think I might get what’s up. So, several
points:

  1. While possible to do thing differently than Rails conventions
    expect, Rails works best if you follow convention, since it assumes
    you will. One issue may be that Rails’ assumption is that the primary
    key field of a table is called simply “id”, instead of something
    different like uid as in your example. If its not, you can do a
    migration using rename_column to change uid to id.

  2. The trick here is aligning the values from the form (those in the
    params hash) and the actual Member instance to be found (maybe) using
    the find method. In params you have not just a name and an email, but
    (if the form is defined as such) an instance of the Member object,
    even if not complete. And you can use this object to determine if its
    contents identify an actual member from the database. When found, a
    member will be returned as an object containing all of the member
    data. If you set up your form as a view of the Member class, you
    could do something like Member.find(:first, :conditions => “name
    = :name and email = :email”, params[:member]) when the form is
    submitted. In other words, if your form is defined as a view of a
    member (even if not all of the fields), it’s values will be stored in
    a hash that is one key , named :member, of the params hash. You can
    get at specific fields of the object from params like params[:member]
    [:name] if you need to, but rails is smart enough to say, “hey, I have
    a member object in params, let’s see if I can find a :name and :email
    field” to use for my condition. Is the form a view of Member? It
    should be if its fields are defined as <% form_for :member … %>, or
    some variant of that.

For those of us who come from a strict, tight-binding, declarative
world (Java, C#, SQL), this kind of loosey-goosey stuff can be
maddening. There are at least 5 other ways to do the thing you want
to do, and probably more. The trick is to figure out what “the flow”
is, and then go with it! It’s not so hard to go with the flow, but I
am still working on figuring out what the flow actually is (hard,
since it’s not well documented, and in significant flux at the
time :-). All I can say is, if you don’t have a copy of Agile Web
Development with Rails (AWDWR), get one! It’s the only source I have
found that describes, explains, and documents (not to mentioned
provides a good index for) most of what is going on in Rails.

By the way, everything I have written is my potentially flawed attempt
to translate your question into an example found in AWDWR combined
with less than a month of actual experience. So take my words with a
grain/pound of salt :slight_smile:

Tom

At first, thanks a lot!!
And I’m not good at speaking in English.
I wish you understanding, please. :wink:

I almost have programmed using PHP(not OOP).
In my memory, concept of structured programming is very natural.
It’s very hard for me to program with OOP languages like java, ruby…

I think The Most Important thing is the Basic concept of it.
I’ll try to study more and more about the base knowledge about Ruby
and RoR.

Thanks, again.
Rucy.