在rails中移动代码的难度

时间:2013-03-28 18:50:00

标签: ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我在控制器中有以下代码

def search 

if params[:name]
      response = client.get_games(params[:name])
      if response.is_a?(Net::HTTPSuccess)
        games = client.parser(response)
        unless games.empty?
          session[:games] = games
          redirect_to game_path(params[:name])
        end
      end
....
....
....
end
end
I have the following code written in my client

def get_games(name)
    find_games(CGI.escape(name))
 end

我发现在我的客户端中将一些逻辑移动到get_games方法时遇到了困难。例如,这样做对我不起作用,我想就如何进行或如果我做错了一些建议

if params[:name]
  games = client.get_games(params[:name])
  unless games.empty?
    session[:games] = games
          redirect_to game_path(params[:name])
        end
      end
...
...
...
end
end

def get_games(name)
response = find_games(CGI.escape(name))
if response.is_a?(NET::HTTPSuccess)
return JSON.parse(response.body)
else 
return nil
end
end

前一种情况有效但后者不起作用。基本上我只需要帮助将def search内的代码移动到客户端,并且在我的控制器中只有redirect_to game_path(params[:name])。任何帮助非常感谢

1 个答案:

答案 0 :(得分:0)

您正在使用

response = client.get_games(params[:name])

在第一种情况下。如果get_games是客户端模型的方法,那么很可能你应该改变

response = find_games(CGI.escape(name))

response = get_games(params[:name])

如果没有更多代码,很难回答。

相关问题