Ruby on Rails - Access Controller Variable from Model

Ruby on Rails - Access controller variable from model

You shouldn't generally try to access the controller from the model for high-minded issues I won't go into.

I solved a similar problem like so:

class Account < ActiveRecord::Base
cattr_accessor :current
end

class ApplicationController < ActionController::Base
before_filter :set_current_account
def set_current_account
# set @current_account from session data here
Account.current = @current_account
end
end

Then just access the current account with Account.current

How to access instance variable from a Rails model?

You will have to pass the team as an argument to your class.

class EventsCreator
attr_reader :team
def initialize(team)
@team = team
end

def some_method
puts team.name
end
end

# Then in your controller you can do this
def create
EventsCreator.new(@team)
end

If you plan on including ActiveModel::Model then you can just do

class EventsCreator
include ActiveModel::Model
attr_accessor :team

def some_method
puts team.name
end
end

# And then in your controller it's the same thing
def create
EventsCreator.new(@team)
end

Access a variable defined inside a model's method in a controller.

This is an object orientated programming question; you're not calling a variable from your model, you're accessing either an attribute or an instance value (very important in terms of scoping etc) from a class.

--

You'll either need to make the variable a class variable, invoke it as an instance method, or have class method to return it:

#app/models/model.rb
class Model < ActiveRecord::Base
cattr_accessor :sum #-> Model.sum class variable (static)
@@sum = 10

def self.sum
10 #-> Model.sum class method (static)
end
end

What you do depends on what type of data you're looking to return.

  • If the data is static, use a class method / variable
  • If the data is dynamic, use an instance method

Above is the code you'd use if you want to return a static value.

If you wanted to return a dynamic value, you'd use:

#app/models/model.rb
class Model < ActiveRecord::Base
def score
self.games * self.goals # -> @model.sum instance method (dynamic)
end
end

--

The difference is that if you use a class value, it is only available through initialization of the class. IE you can call Model.sum and have access to the record.

Instance methods / values are only accessible through an instance of the class:

@model = Model.find 5 #-> creates a new instance of the class
@model.sum #-> pulls the dynamic sum value of the class instance

Fix

In your case, you'd be best using an instance method:

#app/models/abc.rb
class Abc < ActiveRecord::Base
def score
10
end
end

#app/controllers/first_controller.rb
class FirstController < ApplicationController
def get_score
@abc = Abc.new
@abc.sum #-> 10
end
end

This way, your data will be dynamic, allowing you to manipulate it etc.

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

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.



Related Topics



Leave a reply



Submit