如何将Django app转换为RoR?

时间:2017-07-29 16:30:42

标签: python ruby-on-rails ruby django ruby-on-rails-3

我刚刚发现我无法在cPanel上部署Django所以我将它转换为Ruby on Rails。这是一个小应用程序,用户可以注册一个青铜,银色或金色的服务包。

我确实想将此流从Django转换为RoR:

DJANGO CODE:

URL:

x

视图:

public class Foo {
    static final int x = 18;

    public void go(final int y) {

        // This is not possible because 'x' is final,
        // however the state of 'y' does not matter,
        // because its value is not being changed
        x = y;
        System.out.println(x);
    }
}

public class Mixed2 {
    public static void main(String[] args){

         Foo f = new Foo();
        f.go(11);
    }
}

索引页面上的模板,该模板链接到具有级别的注册页面(在本例中为青铜色):

import java.util.*;

public class Main_5 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
         int m = sc.nextInt();
        for(int i = 0; i < m; i++){
            String password = sc.next();
            System.out.println(got(password));
        }
    }

    public static String got(String password) {
        HashMap<Character, Integer> checkpass = new HashMap<>();
        Character ch = null;
        Integer val = 0;
        int odd = 0, even = 0;
        for (int i = 0; i < password.length(); i++) {
            ch = password.charAt(i);
            if (checkpass.containsKey(ch) == false) {
                checkpass.put(ch, 1);
            } else {
                 val = (Integer) checkpass.get(ch);
                checkpass.put(ch, val + 1);
            }
         }
         Set<Character> hashval = checkpass.keySet();
        for (Character key : hashval) {
             val = (Integer) checkpass.get(key);
            if (val == password.length())
                return "YES";
            else if (val % 2 == 1)
                odd++;
            else
                even++;
        }
        if (odd == 1 || odd == 0)
            return "YES";
        else
            return "NO";
    }

}

这是我目前为RoR所做的:

routes.rb中:

#level is bronze, silver or gold
url(r'^signup/(?P<level>[\w\-]+)/$', views.signup, name="signup"),

控制器:

# subscribe to gold/silver/bronze package
def signup(request, level):
    """ when users subscribe to a package """
    context_dict = {}
    context_dict['level'] = level
    return render(request, 'payligent/signup.html', context_dict)

视图:

<a href=" {% url 'payligent:signup' 'bronze'%} "><button class="btn btn-success">Get Started</button></a>

但是,我在RoR服务器上收到此错误:

  

没有路线匹配{:action =&gt;“pricing”,:controller =&gt;“welcome2”,   :level =&gt; nil}缺少必需的键:[:level]

1 个答案:

答案 0 :(得分:0)

<a href="<%= link_to 'package_signup bronze', package_signup_path('bronze') %>"><button class="btn btn-success">Get Started</button></a>

get 'pricing/:level', :to => 'welcome2#pricing', as: "package_signup"

此路线告诉您的应用,它期待路线路径,例如/ pricing / bronze。因此,为了使助手package_signup_path正常工作,您需要提供最后一位(:level)。

然后,在您的控制器上,您将为您动态设置参数:level

您在行动中的意思是,您可以访问它 - 如果您通过转到.../pricing/bronze访问您的应用:

class Welcome2Controller < ApplicationController
  def pricing
    @package_signup = params[:level] # <- that will yield 'bronze' because rails translates that portion of the url that was entered and matched, to the name you gave it, you don't need to pass nor declare additional variables nor function parameters
  end
end

现在,这意味着您的视图中可用的实例变量会产生&#39; bronze&#39;或者用于访问此特定路线的任何内容。现在意味着您的路径助手将按照您的方式工作。

<a>代码会将您链接到同一页面,所以不确定您想要的是什么?