Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Session variable


  • Please log in to reply
4 replies to this topic

#1 Luis Manrique

Luis Manrique

    Passenger

  • Members
  • 3 posts

Posted 13 January 2014 - 06:06 PM

Hi, my name is Luis, I'm totally new to ruby/rails and I'm trying to learn through a hands on programming a museum software to record researchers data in a database (mysql).

In my software I need to record a integer (research_id), in a session and I have no idea how to do that. In a index page I have a list of all records and I want to create a link like "show","edit","destroy", this link will be called "use" and this link will record a id from specific line and redirect to other url.
 



#2 RoyTheB0y

RoyTheB0y

    Signalman

  • Members
  • 10 posts

Posted 13 January 2014 - 08:17 PM

Not really sure what you are after. If it helps Rails accesses cookies via session[] which enables access data across requests.

http://guides.rubyon...ew.html#session

so you can do

session[:research_id] = @research_id

 

@research_id = session[:research_id]

 

are you sure you want a session variable rather than keeping track of users and storing the information in your database



#3 Luis Manrique

Luis Manrique

    Passenger

  • Members
  • 3 posts

Posted 13 January 2014 - 08:42 PM

I want to keep this integer in a session because after log in, a user can choose one "pesquisa" as shown below, and with this value, the user can list all other tables that have the same value  as the session, actually I will use to store the foreign key for all another tables, but I insist, I'm newbie and I accept sugestions, even to delete everything and restart a new project. Thanks for the answer.

 

 

 

alphamuseum_2014.png



#4 Luis Manrique

Luis Manrique

    Passenger

  • Members
  • 3 posts

Posted 15 January 2014 - 04:53 PM

To clarify what I need, in my index.html.erb file, I have this code with these links, and I need to create another one that will record a research.id in a session. How can I create a link who record an integer in a session? Thanks!!!

 

  <tbody>
    <% @researches.each do |research| %>
      <tr>
        <td><%= research.nome %></td>
        <td><%= research.orientador %></td>
        <td><%= research.palavraschave %></td>
        <td><%= link_to 'Show', research %></td>
        <td><%= link_to 'Edit', edit_research_path(research) %></td>
        <td><%= link_to 'Destroy', research, method: :delete, data: { confirm: 'Are you sure?' } %></td>
       ??? <td><%= link "Usar", session[:pesquisa] = research.id %></td> ???
      </tr>
    <% end %>
  </tbody>



#5 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 27 January 2014 - 05:32 PM

You need to link to a controllers action and pass the research.id in as a parameter so that it is received in the params hash in the action.

The action in the controller would then shovel the id into the right session key.

<%= link_to "Usar", some_path(research) %>

So it's just like a standard show link

 

Then in the relevant action that you route to the path you just assign the params[:id] to the session key you want to hold it in

 

e.g.

session[:research_id] = params[:id]

From yopur question I think you are missing a crucial piece of the web jigsaw.

 

A view is an HTML page right?

A browser runs on a users P.C. or device right?

The web server (unicorn/webrick/whatever) receives a reuquest from a browser, sorts out an HTML page that th browser can interpret and sends to over the web to the user's browser right?

 

So code in your template is not dynamic. Data is injected into the page before being sent to the browser.

In order for the web server to store anything at all it has to be told what to store and how to store it.

This means that the user has to take some action to HTTP POST data back to the server hence the need to send the id back to a controllers action for you to then add to the session (stuff it into a cookie) and then send some more data back to the user in the form of redirecting to another controller/action (url) or updating the current page and sending that straight back to the user. Whatever suits your needs.

 

Therefore you don't add data to the session in the view.

 

I hope that makes some sense.


Programming is just about problem solving!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users