2013-12-16 37 views
0

我使用设计3应用程序,我已经实现了一些guest用户funcitonality所概述here设计guest用户 - 在我的Rails更新的属性

我在这里的问题是,我的应用程序依赖于一定程度上精确地理编码(使用Rails地理编码和地理编码用户通过ZIP),所以我不希望使用的IP地址。我的解决方案是他们进入他们的邮政编码和访客用户记录与拉链创建和地理编码lat和长有来宾用户的登录页面。请记住,这些访客用户记录尚未创建,但不知道为什么他们没有创建,直到离开目标页面。我试图简单地用一个形式提交称为create_guest_user:zip作为参数,但是这是一个私有方法,所以我不能这样做。

<p> 
To get started, either <%= link_to 'Sign Up', new_user_registration_path %>, 
    <%= link_to 'Sign In', new_user_session_path %> or enter your zip code here if you want to look around first: 
    <br><br> 
    <%= form_tag({controller: "application", action: "create_guest_user", method: "post"}) do %> 
    <%= text_field_tag :zip %> 
    <%= submit_tag 'Enter Zip', class: "btn" %> 
    <% end %> 
</p> 

从我的阅读中,我认为最好是将此方法保留为私有,所以我想弄清楚如何使这项工作。

上创建一个使用在上面的链接列出,但包括设计方法,该访客用户的最佳方式有什么想法:拉链作为由来宾用户指定的PARAM?

请让我知道,如果我能在这里澄清什么,在此先感谢!

回答

0

从您的链接看,方法current_or_guest_user,它只是调用guest_user,其中调用create_guest_user

如果你想离开create_guest_user,然后只需创建用于处理表单后控制器动作,它可以调用create_guest_user因为你的控制器方法应该扩展ApplicationController,这哪里是create_guest_user方法声明为private。

class ApplicationController < ActionController::Base 

    def public_method 
    # you can call private methods from inside the same class so this is valid 
    private_method 
    end 

    private 

    def private_method 

    end 
end 

class LocationController < ApplicationController 

    def location_public_method 
    # you can call private methods from ApplicationController since LocationController extends ApplicationController so this is valid 
    private_method 
    end 
end 
+0

这听起来像它可能适用于我。有一个问题,从公共方法调用'create_guest_user'是否有效地使'create_guest_user'成为公共方法? – settheline

+0

不会。如果你没有用公共方法调用它,你会怎么称呼一个私有方法?一种方法是私人的,因此它不能从课外被调用。这是引用Java,但它仍然有帮助.http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private – Catfish

+0

嗯......不知道因为我对轨道和编程一般都比较陌生。我只有在before_filters等方面有经验调用private方法。我不知道这些是否属于“公共方法”,因此我的问题。 – settheline