I digg into some project with long development history
Maybe it's newbe question:
For specific controller I found thats it inherited from ActionController
like so
class GraphController < ActionController
end
and seems it used for API cause when I add some actions there they shown without layout
can anybody point me to some good article about ActionController usage.
I check documentation but can't find any good explanation for tats stuff
http://guides.rubyonrails.org/action_controller_overview.html contains a good description of ActionController basics.
Related
Absolute beginner with ruby and with rails. My first full blown project will interface with an existing system through a REST api.
So it doesn't employ the normal ActiveRecord model and I was hoping for some examples of projects that replace the normal use of models with API calls. The entire CRUD set will require the app to make the corresponding API calls. I'd like to do this the right way, just don't know what that looks like yet.
Thanks! :)
Helpful links for googlers:
http://yetimedia.tumblr.com/post/35233051627/activeresource-is-dead-long-live-activeresource
https://github.com/rails/activeresource
I don't know of any projects that are open source that do this, but I'd recommend looking into Active Resource. It was part of Rails through 3.2, but has been moved to a separate repo for Rails 4. The idea is that it lets you replace database persistance with RESTful APIs, which sounds like exactly what you're trying to do. There is some documentation here which should give you enough info to get off the ground. There's also a pretty old Railscast on it, but I'm sure the concepts haven't really changed that much.
Here's an example from the docs that shows how to setup your "model":
class Person < ActiveResource::Base
self.site = "http://api.people.com:3000"
end
I'd suggest leveraging ActiveModel so your models aren't 'AR' backed, i.e. no persistance via AR. You'll handle CRUD accordingly in your controllers and craft your own 'business logic' that you can tuck into your ActiveModel.
I've been going through Ruby (and Ruby on Rails) tutorials online such as RubyMonk, Codeschool courses, the Railstutorial, and Learn Ruby the Hard Way.
These are all excellent resources, but I feel like I need to see some Ruby code that is actually being used in production in order to advance to the next level. I would assume that a repo like Rails would be considered advanced level - so I'm looking for something a little in-between.
Are there some GitHub public repos that are using a more intermediate difficulty level of Ruby coding that I can learn from?
If you can recommend some that would be an appropriate follow-up based on the contents of the tutorials I listed earlier, that would be great.
I've looked some myself, but I'm asking for recommendations because I want to make sure I'm studying something that is "the right way" to do Ruby and not learning bad habits.
Here's two I've created:
http://github.com/rails3book/ticketee - A light-weight project management system from the http://manning.com/katz'>Rails 3 in Action book.
http://github.com/radar/forem - A forum engine for Rails 3 applications.
Both should have pretty good examples of intermediate code, along with tests.
I published a solution to a ThoughtWorks interview problem that I used as the example for a talk. I'm not sure it's "intermediate", but quite a few things in there might fall outside the range of "beginner", so I think it might be up your alley. There's also a rather robust test suite using MiniTest::Spec and mocha.
For example, I use this approach to cause classes which include a module to register themselves with their base class:
module TaxWorks
module TaxStrategy
def self.included(base)
add_strategy base
end
def self.strategies
#strategies ||= []
end
private
def self.add_strategy(s)
strategies << s
end
end
end
In this example, now you can do TaxStrategy.strategies to enumerate the list of possible strategies, after loading any you care about.
Hope that helps!
If you're into Rails you should have a look at these open-source Rails apps:
https://github.com/rubygems/rubygems.org - Source code for rubygems.org, the Rubygems server/website
https://github.com/ryanb/railscasts - Source code for railscasts.com
Both of these apps are exceptionally well organized, clean and thought out. In the case of rubygems.org it's had tons of contributions from different rubyists and is a great working example of Rails in action.
I think Sinatra is a good start. Take a look at Sinatra's source code and build a small MVC application with some helpers. You will not only learn some ruby but also get a good understanding for how Rails work without the magic. It is made "the right way" and you will not learn any bad habits.
I'm a new Ruby/Rails guy. Here's one question puzzling me:
Can we find the exact module lists mixin-ed for a class in Rails from the API doc? For example, if we have an instance of one subclass of ActiveRecord::Base, we can use validates method in this class such as following:
class Product < ActiveRecord::Base
has_many :line_items
validates :title, :description, :image_url, :presence => true
end
from rails api doc we can find that validates belongs to ActiveModel::Validations::ClassMethods, so ActiveRecore::Base must have ActiveModel::Validations::ClassMethods mixin, but I didn't find anything relating to this in the api reference. Can anyone tell me if I can find this info from api doc?
Thanks for all of your help in advance. I really hope my question doesn't sound too silly:)
in console :
ActiveRecord::Base.included_modules
I don't see anything like that in any of the Rails documentation I've seen:
http://api.rubyonrails.org/
http://apidock.com/
http://rubydoc.info/
And with monkey patching, there probably isn't any way to know until Rails is all cranked up and you have an actual ActiveRecord::Base in your hands; for example, the PostgreSQL, MySQL, and SQLite adapters re-open ActiveRecord::Base and they might include extra modules along the way; other gems you're using and even your own code may do similar things.
If you just want to know the standard set of mixins, use the source (Luke!) or do as maniek suggests and ask ActiveRecord::Base in the Rails console.
In general, the Rails API documentation isn't that useful unless you already know what you're looking for and even then, digging into the source is often necessary.
Your best bet is to check the guides for what you need and read the source if that doesn't work.
Short Answer: You can't get your list from the documentation because the modules cannot be known until run time. Read the guides and the source if you need to know something.
Short answer is that you can't.
A long answer is merely an excuse (a lousy one): Ruby and Rails documentation is generated automatically, from the source. And Ruby is notorious for its metaprogramming abilities, so the fact that a method is included into a class can not be (easily) extracted by parsing the source. The only reliable way to get the list of methods in a class is to fire up a console and type SomeVeryImportantClass.methods.
That said, it really is discouraging and frustrating to both new and experienced developers that one can't easily find in the docs some of the most frequently used methods. It is dissapointing that someone didn't think of a way to somehow include these cross-references, perhaps even manually.
in a controller i can use request and params methods.
i know each controller is inheriting from ActionController::Base.
however, i cannot find these 2 methods in the api documentation for ActionController::Base http://api.rubyonrails.org
where are these methods defined? it would be great to know ALL methods that i can use in a controller.
thanks
request and params in ActionController:Base are shortcuts to the ActionController::Request object and its parameters method. It isn't well-documented, but you can see that by looking at the code for ActionController::Base.
Knowing that, you can find the documentation for these methods here:
http://api.rubyonrails.org/classes/ActionController/Request.html
Are you looking at the Rails 3 docs or the Rails 2 docs? I like to read the docs through this site: http://railsapi.com/doc/rails-v3.0.0.beta.3_ruby-v1.9/
If you go to the root, it lets you set your version of rails and ruby, so you have the right doc stack, with a good search feature etc. Maybe that will help :)
I read an article here and I used the principles found there to write a administration check for my controllers. Basically it checks to see what controller you're in and then I create an array saying if this person as permission to see that controller, than find, pass it on.
The author of the article said that readers shouldn't use the example code as it was flawed and that they should use his plugin (which of course is outdated now). And he said
"there are some issues with the above code when running in production mode due to the use of class variables and class caching."
I have a test failing and I think it might be due to this but I'm not quite sure what part of the code I shouldn't be using. I had the following (in application.rb):
controller = params[:controller]
action = params[:action]
But I changed that to:
controller = self.class.name
action = params[:action]
How do I access the action name correctly? And in general what kinds of things can you do in development mode that you can't do in production?
Have you considered a role based permissions plugin instead?
See the following stackoverflow question:
Which Rails plug in is best for role based permissions? (Please provide one nomination per answer)
I'm unaware of any issues with production usage of the code you have pasted in. The plugin in question might have some issues due to a possible misuse of ## however, I've not inspected it thoroughly.