Hi, community. I have just recently started learning RoR so forgive me if my questions are too simple. Well, I have a login page and login controller. Login page have login text field. I want to bind its value to controller field. And if login incorrect that fields repopulate with previous values. I expected to ses something like this: Page: text_field(''email'') #bind with @email property of LoginController Controller: class LoginController < ApplicationController def login puts @email #Get email value end end But seems that RoR require that user create model object that will store this fields. At least in AWD I saw only way of using form fields text_field(''user'',''email'') Controller def login map_of_user_params = params[:user] @user = User.new(map_of_user_params) end I am OK with having 2 params in text_field but the problem with this solution that I have no model class that holds operations with user. Create class only for this method seems a bit awkward for me. I have tried to store in property map of parameters def login @user = params[:user] end but repopulating fails with exception undefined method `email'' for {"password"=>"pwd", "email"=>"mail"}:HashWithIndifferentAccess As I understand that on form fields repopulating RoR tries to access to property through method name (LoginController.user.email). But hash no ''email'' property. Well I am not sure that I am on the right way (if so please point me how to manage forms w/o Model classes) but I expected that if object have no method and its instance of Hash then try to access via object[:proprtyname] What do you think? -- anatol _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Anatol Pomozov <anatol.pomozov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> Well I am not sure that I am on the right way (if so please point me > how to manage forms w/o Model classes) but I expected that if object > have no method and its instance of Hash then try to access via > object[:proprtyname]A couple of points. First, it''s easy to add fields onto the User class even if you don''t want to store them in the database. That may be the easiest way to deal with this. The ''email'' attribute could then be associated with the temporary User object for the purposes of displaying on the form. class User < ActiveRecord::Base attr_accessor :email end Then in your view you just do: <%= text_field ''user'', ''email'' %> as you''ve already seen. To recap, attr_accessor is a Ruby method that adds a read/write attribute to a class. This won''t be stored in the database with the User object. Another option is to use text_field_tag rather than text_field. This lets you specify the default values rather than pulling it from a defined object. <%= text_field_tag :email, params[:email] %> You can see examples of this on page 360 (of my copy, I''m not sure it''s the final version) of AWDwR in the section titled "Working with Non-model Fields" in section 17.8 Form Helpers. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org