SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A few Rails noobie questions

    I spent the weekend playing with rails and I ran into a few problems that I think should be fairly obvious but that I could not find answers for.

    • I built in some basic user authentication, with the user object being stored in the session variable. I want to copy this to the @user global variable on each page load. Currently I do this with a before_filter, is that the right way to do it? It doesn't seem like the right way, since it is not actually a filter, but it works.
    • I have a form based on database fields from two different tables, with the results kept in the session to be saved later instead of saved right away. How do I validate a form that is not being saved right away?
    • Also, how do I set default values of form elements (eg. text_field)?
    Paul Butler.org
    JSSpamBlock - Reduce WordPress spam.

  2. #2
    SitePoint Member
    Join Date
    May 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) A before filter is fine for this IMO
    2) You can call Model.valid? which will return true or false if the model validates in its current state
    3) If you are using a form_for you can simply set the variable in the controller:

    Code:
    @model = Model.new
    @model.variable_name = "whatever"
    HTH

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks atech.

    Quote Originally Posted by atech View Post
    1) A before filter is fine for this IMO
    Ok. It seems to work fine, so as long as it doesn't have any side effects it should be fine.

    2) You can call Model.valid? which will return true or false if the model validates in its current state
    Cool, I will try this. I wonder if it automatically sets the errors so that error_messages_for will show up, because ideally that is what I would like to happen. I will see when I try it.

    3) If you are using a form_for you can simply set the variable in the controller:

    Code:
    @model = Model.new
    @model.variable_name = "whatever"
    I am actually using start_form_tag. I tried doing something similar to this with no success, but I think it may be because I did not properly create the object.

    Thanks a lot, I am starting to see the intuitiveness of rails as I work through my problems.
    Paul Butler.org
    JSSpamBlock - Reduce WordPress spam.

  4. #4
    SitePoint Member
    Join Date
    May 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest taking a look through the Rails API documentation - it should help you understand how these functions all work:

    http://api.rubyonrails.org/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •