Forum: Ruby on Rails Login form_tag with select_tag option

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tony P. (Guest)
on 2009-01-27 13:52
Hi all,
In the application i develop, a sophisticated login system is required.
Being a newbie in Ruby, i'm not comfortable with the select tag in the
form tag.
My requirement:
2 tables - User(LoginName, Password, RoleId...) and Role(RoleId,
RoleName...)
View - RoleNames to be loaded in the drop down box and On submit, RoleId
to be passed to controller for authentication.

My Code Snippet:
#Controller
    if request.post? and params[:login]
      @user = User.new(params[:login])
      validUser = User.find(:first,:conditions => ["login_name = ? and
password = ? and role_id = ?",@user.logname, @user.password,
@user.roleid])
      if validUser
        @role = Role.find(:first, :conditions => ["id = ?",
@user.roleid])
        if @role.role_name == "Admin"
          session[:admin] = validUser.id
          redirect_to :controller=>'admins', :action => 'index'
        else
          session[:user] = validUser.id
          redirect_to :controller=>'projects', :action => 'index'
        end
#View
            <label for="user_login">Username:</label>
            <%= text_field("login", "username", :size=>"20" ) %>
            <label for="user_password">Password:</label>
            <%= password_field("login", "password",:size=>"22" ) %>
             <label for="user_login">Role:</label>
      #Here is the issue
            <% @role = Role.find(:all, :select => "role_name") %>
            <%= select_tag("login", options_for_select(@role)) %>
            <%# collection_select(:role, :id, @role, :id, :role_name) %>
            <%= submit_tag "Login" %>

Could anybody please give me a better snippet to accomplish the same...?

Appreciate your prompt reply...
Frederick C. (Guest)
on 2009-01-27 16:36
(Received via mailing list)
On 27 Jan 2009, at 11:52, Tony P. wrote:

>
> Hi all,
> In the application i develop, a sophisticated login system is
> required.
> Being a newbie in Ruby, i'm not comfortable with the select tag in the
> form tag.

>
> Could anybody please give me a better snippet to accomplish the
> same...?
>
have a look at the (still in progress) guide on forms
http://guides.rails.info/form_helpers.html

Fred
Tony P. (Guest)
on 2009-01-28 05:49
Frederick C. wrote:
> On 27 Jan 2009, at 11:52, Tony P. wrote:
>
>>
>> Hi all,
>> In the application i develop, a sophisticated login system is
>> required.
>> Being a newbie in Ruby, i'm not comfortable with the select tag in the
>> form tag.
>
>>
>> Could anybody please give me a better snippet to accomplish the
>> same...?
>>
> have a look at the (still in progress) guide on forms
> http://guides.rails.info/form_helpers.html
>
> Fred

Thanks a lot Frederick... I was looking for a simple logic. However it
must work.
This topic is locked and can not be replied to.