`
CaiDeHen
  • 浏览: 94310 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论
文章列表
The final piece of the administration puzzle: authentication. There are many different approaches which is why I saved this step for last. This episode will cover a few techniques including the simple solution used for this site. # controllers/application.rb def admin? session[:password] == 'foob ...
In this second part of the series on administration, you will learn how to lock down the site to keep the public from accessing the administration features. <!-- episodes/index.rhtml --> <% if admin? %> <%= link_to 'New Episode', new_episode_path %> <% end %> # controlle ...
Displaying flash messages in the layout can be a pain at times. In this episode you will learn an easy way to display any kind of flash message by looping through the hash. <!-- layouts/application.rhtml --> <% flash.each do |key, msg| %> <%= content_tag :div, msg, :id => key %& ...
It is often asked: how do I create a list of checkboxes for managing a HABTM association? Ask no more because this episode will show you how to do exactly that. <!-- products/_form.rhtml --> <% for category in Category.find(:all) %> <div> <%= check_box_tag "product[cate ...
Keep your controllers clean and forms flexible by adding virtual attributes to your model. This very powerful technique allows you to create form fields which may not directly relate to the database. <!-- users/new.rhtml --> <%= f.text_field :full_name %> # models/user.rb def ...
You can pass more than simple strings to find conditions. Arrays, ranges, and nil values can be passed as well. In this episode you will see the tricks involved with passing these odd objects to find conditions. (Update: audio fixed). Task.find(:all, :conditions => ["complete=? and priority=? ...
Did you know ActiveRecord provides class methods for performing calculations on models? You can even use these methods through associations. Task.sum(:priority) Task.sum(:priority, :conditions => 'complete=0') Task.maximum(:priority) Task.minimum(:priority) Task.average(:priority) p = Proje ...
Be careful when storing a model in a session. It will behave differently than you expect and can easily get out of sync with the database. Instead of storing the model directly in the session, store the id to the model and use that to fetch it from the database.
In the final part of this series you will see how to refactor your tests. Keeping tests clean is important because it will make testing easier to do in the future. # user_test.rb def test_full_name assert_equal 'John Doe', full_name('John', nil, 'Doe'), "nil middle initial" ass ...
Testing and refactoring go hand in hand. Refactoring is all about improving code without changing its behavior. Testing is all about making sure you don't change the behavior while you are improving the code. # models/user.rb def full_name [first_name, middle_initial_with_period, last_name] ...
Learn how to clean up your code through refactoring. This episode will show you how to move code from the view into the model to remove duplication and simplify the view. <!-- show.rhtml --> Name: <%= @user.full_name %> # models/user.rb def full_name name = first_name + ...
Are you accepting sensitive user data? Passwords, credit card numbers, etc. By default, Rails stores all submitted parameters in plain text in the logs. This episode will show you how to filter this sensitive input so it doesn't show up in the log file. # controllers/application.rb filter_paramete ...
If you want to change something in the layout on a per-template basis, content_for is your answer! This allows templates to specify view code that can be placed anywhere in a layout. <!-- projects/index.rhtml --> <% content_for :head do %> <%= stylesheet_link_tag 'projects' %> ...
Everything you wanted to know about layouts: global layouts, controller layouts, shared layouts, dynamic layouts and action layouts. Yes, there really are that many ways to specify a layout. # projects_controller.rb layout :user_layout def index @projects = Project.find(:all) render :layo ...
It may have a goofy syntax, but the Symbol#to_proc feature Rails adds allows you to do simple blocks very quickly and easily. # models/task.rb def self.all_names find(:all).collect(&:name) end
Global site tag (gtag.js) - Google Analytics