Accessing One Controller Variable in Another Controller in Rails

Accessing one controller variable in another controller in Rails

I was going to make this a comment, but I think it is worthy of an answer. The thing to understand about not just rails, but the entire internets, is that HTTP is stateless. This means that in your controller you grab the user's information, build them a webpage with all of their stuff from the database, and send it out to them. Then they do something on the page and send a request in for another page or more data, and your rails app is like "Oh hai! Who are you?" Because all those variables that you filled up with nice information are just gone. Every time your app receives a request, it makes a brand new instance of the controller it is using, and then gets rid of it afterwards, along with everything inside it. And this is important, because there might be thousands, or millions of people currently logged into your ultimate frisbee league website trying to figure out who is playing where on Saturday. And you want to give the right info to the right person, you can't assume that the person you last sent information to is the same person asking you for new info now.

So we need to use the session, some information that gets passed to the client browser every time you send out a page, and the browser sends it back to you with every request. You put enough info in the session so that you can use it to rebuild what you need from the server when you get a response. I'm going to make this real simple and then refer you to the Rails Security Guide where you can get some more important information. Actually, I'll just use their example:

You can add the user id to the session this way:

session[:user_id] = @current_user.id

And then get it back this way:

@current_user = User.find(session[:user_id])

And then you use your user model (in this case) to get the specific info you need from the database. Here's another link to the Rails guides because it's so nice, I'm linking it twice: http://guides.rubyonrails.org/security.html#what-are-sessions-questionmark

If you are doing something that requires a password you can use a gem like devise, instructions here, but be warned, it totally hijacks your user model, your sessions controller, some views, basically all things related to logging in and identity. It isn't easy to customize, but it makes all this stuff and things much more complicated very simple to implement. If you don't need any usernames or passwords and you are doing something real simple just skip the special gems and use the session yourself.

access variables from another controller

In your MenuPricesController you can create a new Category instance and use it like this :

class MenuPricesController < ApplicationController
before_action :set_menu_price, only: [:show, :edit, :update, :destroy]

def index
@menu_prices = MenuPrice.all
@categories = Category.all
# do something
end

How do I access an instance variable from another controller?

UPDATED TO USE A SINGLE DESIGNER RECORD

You should use associations. Add this to your models.

class Designer < ActiveRecord::Base
has_many :projects
end

class Project < ActiveRecord::Base
belongs_to :designer
end

Your view should look like this:

<% @designer.projects.each do |project| %>  

<% end %>

More info on associations here:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Rails: Passing a variable from one controller to another for two forms

you need to pass in the patient_id in your redirect in the create action of PatientsController.

redirect_to '/referral_requests/new'

should become

redirect_to new_referral_request_path(patient_id: @patient.id)

then in ReferralRequestsController you can associate the newly built referral_request with the patient

def new
@patient = Patient.find(params[:patient_id])
@referral_request = current_user.referral_requests.build(patient: @patient) if logged_in?
end

Then in your referral_requests/new.html.erb form you can add a hidden_field_tag

form_for(@referral_request) do |f|
f.hidden_field :patient_id
.....
end

which will then add patient_id to into the form params and pickup the patient_id that got associated in the new action of the controller

You will need to add :patient_id into your referral_request_params method as well

Access a variable in a controller from another controller

You cannot as the other controller does not/may not live during the execution of the correct controller. Even if it was possible logically one controller have nothing in common - IMHO breaking modularity of MVC pattern.

Refactor code to lib/, ApplicationController or some model (separate or divided as method) depending of application.

PS. I'm not quite sure what you mean by "Data Picker" but it sounds like something that should be implemented in model(s).

Ruby on Rails: how to pass an instance variable from a controller to another controller via a form

Any reason why you are using a GET to post a form? Change it to a post and grab the name via params[:name] in the action the post is being routed to. You instantiate the instance variable initially and you are actually posting to your method instantiate.



Related Topics



Leave a reply



Submit