views Tag
I’ve recently seen several requests come through from people trying to figure out how to call controller methods from their views. The answer is pretty simple. Your controller instance, which is calling your view is stored in the instance variable @controller. So, calling public methods is as simple as this:
1
| @controller.public_method |
To call a private method you can do this:
1
| @controller.send("private_method", args) |
You should note that if you’re calling controller methods frequently in your views, you should consider putting them into helpers to make them available that way. If you need to manipulate or manage some data, the controller is the place to access the models for this, not the views.
action, controllers, method, views
Last week I started the 31 days to build a better blog on this blog. The week before I started—May 31 through June 6—the blog got 28 visits and 34 page views.
I’ve completed the first two days’ assignments. It was very helpful in a couple of ways. First, it helped me define exactly what I wanted to do with this blog. It also made me realize that my about page wasn’t visible from my wordpress theme, so I found a new theme and reworked my site. Finally, it guided the writing of one of my posts, which I think was very helpful both to the readers and for me as a writer. Feel free to let me know what you think so far. I’ll post the changes in traffic next week regarding these effects.
As of right now, I’ve gotten 34 visits and 42 page views this week—since the beginning of day June 7.
blog, blogging, help, search engine optimization, seo, traffic, views
Have you ever wished you could mix Rack or Sinatra into your Ruby on Rails application just to get its raw throughput on certain parts of your application?
Let’s face it, sometimes, the Rails framework is overkill when we’re returning a simple string or an object in JSON as our response. Your answer for these instances is here. Rails Metal.
Performance
Some people have reported huge speed increases in Rails Metal over the Rails MVC framework. This article claimed a 25x increase over Rails. Pratik Naik from the Rails Core team benchmarked a more believable increase of 4x. Whatever the case, the performance advantage is worth noting.
Read More
activerecord, controllers, erb, HTML, http, middleware, rack, Rails, Rails Metal, response, Ruby, views
Have you ever wished you could call yield multiple times in the same layout in order to get extra stylesheets, javascript, or a dynamic sidebar? Having worked on several projects that needed this soft of functionality, I’ve probably built this in three or four times. Then I started watching railscasts.
There’s a method called content_for that provides this functionality. All you have to do is call content for in your view. Here’s the erb markup:
<% content_for :head do %>
<%= stylesheet_link_tag :my_style %>
<% end %>
Then, in your layout all you have to do is call yield like you normally would in your layout with on small difference. Here’s the erb markup.
I picked up this tip from Ryan Bates Railscast Episode #8
content_for, erb, layout, Ruby on Rails, views, yield
When I started writing part II, I started writing about models. As I got a little further along, I realized that it would be more helpful to provide an overview of the controller, which provides the data that goes into your web page, before I showed you how to get the data out of the database. My hope is that you’ll read this thinking of how you want the data provided, which adds context to part III on models.
Controllers are Where the Work Gets Done
Have you ever worked with one of those people who knows exactly where to go to get everything he needs. Can delegate his tasks effortlessly, and then pull it all together in the end. That guy would be our controller. When your website’s user browses to the page, the Rails engine picks up the request and decides which controller to send it to. More specifically, it decides which method in the controller to send it to. The methods on the controller are referred to as actions.
Read More
controllers, Database, erb, HTML, Rails, Ruby, Ruby on Rails, tutorial, views