On Saturday, May 3, 2014, Sebastjan Hribar <sebastjan.hri...@gmail.com>
wrote:

>  Hi guys,
>
> just a little feedback from a beginner. I've finished my first camping
> app. Thank you for your help and guidance.
>
> The finished app is about quality management and enables users to fill out
> forms. Based on those forms different quality parameters are recorded per
> user and per project. These can be called up in summary reports for desired
> time frame and user. Along with all the forms for a period the average
> quality evaluation is also displayed. Reports can only be generated by
> quality managers.
>
> I've managed to reuse only one html form for creating, viewing and
> editing. In addition, I've setup the form in such a way, that submitting is
> possible only when the form was called via »new« or »edit« route and the
> user has appropriate authorizations.
>
> I have one question regarding the session. I don't actually know how to
> leverage or use it. According to the reference the session adds states to
> the app. Can someone explain a bit more?
>

Sorry for the late reply.

Here's an example of state:

  module App::Controllers
    class Index
      def get
        if @state.user_id
          "Welcome user number #{@state.user_id}"
        else
          render :login
        end
      end

      class Login
        def post
          # Just log everyone in. No passwords here!
          @state.user_id = @input.user_id
          redirect Index
        end
     end
   end

  module App::Views
    def login
      form action: R(Login), method: :post do
        label do
          p "User ID:"
          input name: :user_id
        end
        button "Log in"
       end
     end
  end

Here's an example of the flow when the user visits the site:

1. User visits GET /. Index#get is called.
2. The state is empty, so the #login view is rendered
3. The user enters his user ID (let's say "5").
4. When the user presses "Log in", the browser will do a POST /login with
user_id=5 as parameters
5. Login#post is invoked. This sets the state variable and then redirects
to /.
6. The user's browser then shows GET /. Now he gets a welcome message, not
the login form.

Notice how the same request is done in both step 1 and 6 (GET /), but the
second time the user gets a completely different page. That's because the
state is different.

You might know about cookies: sessions are like cookies that only the
server know how to set. It's impossible for the client to set its own
session; every session has to be set through the @state-variable in an
action.

I hope this clarifies state/sessions a bit. Don't hesitate to ask more if
you're confused.

// Magnus


-- 

// Magnus Holm
_______________________________________________
Camping-list mailing list
Camping-list@rubyforge.org
http://rubyforge.org/mailman/listinfo/camping-list

Reply via email to