Rails 3.2中的DRY控制器 [英] DRY Controllers in Rails 3.2

查看:56
本文介绍了Rails 3.2中的DRY控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行代码气候分析之后,我发现我的控制器并非像DRY那样干燥。
这样的方法:

Following code climate analysis, I found that my controllers are not DRY as it could be. The methods like:

   def index
    @animals = current_user.animals.valid_animals.search(params[:search], params[:page])
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @animals }
    end   
 end

在所有控制器中基本相等。

Are basically equal in all the controllers.

基本上,脚手架导轨生成的代码相同在所有控制器中。
如何以一种真正好的方式使其更清洁和干燥?

Basically, the scaffold rails generated code are "the same" in all controllers. How can I made it more clean and dry in a real good way?

预先感谢

推荐答案

您可以将 respond_with 用于

class AnimalController < ApplicationController
  respond_to :html, :json

  def index
    @animals = current_user.animals.valid_animals.search(params[:search], params[:page])
    respond_with @animals
  end   
end

这篇关于Rails 3.2中的DRY控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆