Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 重构rails fat控制器_Ruby On Rails_Refactoring - Fatal编程技术网

Ruby on rails 重构rails fat控制器

Ruby on rails 重构rails fat控制器,ruby-on-rails,refactoring,Ruby On Rails,Refactoring,如何重构以下控制器获取操作,以及如何在获取操作中使用本地变量(website而不是@website,因为它没有视图,所以是本地变量)并启动获取方法 def fetch @website = Website.find(params[:website_id]) retun redirect_to :back, :alert => t('crawl_counter_error') if @website.fetch_counter < 1 aut

如何重构以下控制器获取操作,以及如何在获取操作中使用本地变量(
website而不是@website
,因为它没有视图,所以是本地变量)并启动获取方法

def fetch    
      @website = Website.find(params[:website_id])
      retun redirect_to :back, :alert => t('crawl_counter_error') if @website.fetch_counter < 1
      authorize! :update, @website, :message => t("website_authorize_error")
      @website.confirmed? || @website.confirmed_key? ? start_fetching : flash[:error] = t('key_error')
      redirect_to :back
  end

private
def start_fetching
    if @website.working_status
      flash[:error] = t('crawling_in_progress_error')
    else
      @website.start_fetch
      flash[:notice] = t('crawler_success_notice')
    end
  end
def fetch
@website=website.find(参数[:website\u id])
如果@website.fetch\u counter<1,则返回重定向到:back,:alert=>t('crawl\u counter\u error')
授权!:更新,@website,:message=>t(“网站授权错误”)
@网站。确认?||@网站。已确认\u密钥?开始抓取:flash[:error]=t('key\u error')
重定向到:返回
结束
私有的
def开始取数
if@website.working\u状态
flash[:error]=t('crawling\u in\u progress\u error')
其他的
@website.start\u fetch
flash[:notice]=t('crawler\u success\u notice')
结束
结束

这可能会更干净一些

# controller

def fetch
  website.find(params[:website_id])

  authorize! :update, website, :message => t("website_authorize_error")

  status = website.fetch

  flash[status[:type]] = t(status[:message])
  redirect_to :back
end

# move to model/website.rb

def fetch
  status = nil

  if (fetch_counter < 1)
    status = {:error, 'crawl_counter_error'}
  elsif confirmed? || confirmed_key?
    status = {:error, 'key_error'}
  elsif working_status
    status = {:error, 'crawling_in_progress_error'}
  else
    start_fetch
    status = {:notice, 'crawler_success_notice'}
  end

  status
end
#控制器
def提取
website.find(参数[:website\u id])
授权!:更新,网站:消息=>t(“网站授权错误”)
status=website.fetch
闪存[状态[:类型]]=t(状态[:消息])
重定向到:返回
结束
#移动到model/website.rb
def提取
状态=零
if(取数计数器<1)
状态={:错误,“爬网\u计数器\u错误”}
是否确认确认钥匙?
状态={:错误,'key_error'}
elsif工作状态
状态={:error,'正在爬网\u progress\u error'}
其他的
开始抓取
状态={:通知,'爬虫成功通知'}
结束
地位
结束