0

I'm using Devise in my Rails 3 app, and I've implemented some guest user funcitonality as outlined here.

The issue I'm having here is that my app relies on somewhat accurate geocoding (using rails geocoder and geocoding Users by zip) and so I don't want to use IP addresses. My solution is to have a landing page for guest users where they enter their zip code and a guest user record is created with that zip and geocoded lat and long. Keep in mind that these guest user records have not been created yet, not sure why they aren't created until navigating away from the landing page. I tried to simply use a form submit that called the create_guest_user method with :zip as a parameter, but this is a private method so I can't do that.

<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>

From my reading I think it's best to leave this method as private, so I'm trying to figure out how to make this work.

Any thoughts on the best way to create this guest user using the Devise method as outlined in the link above but including :zip as a param that is specified by the guest user?

Please let me know if I can clarify anything here, and thanks in advance!

settheline
  • 3,333
  • 8
  • 33
  • 65

1 Answers1

0

From your link, look at the method current_or_guest_user, it just calls guest_user which calls create_guest_user.

If you want to leave create_guest_user, then just create a post controller action for handling your form and it can call create_guest_user since your controller method should extend ApplicationController, which is where the create_guest_user method is declared 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
Catfish
  • 18,876
  • 54
  • 209
  • 353
  • That sounds like it might work for me. One question though, does calling `create_guest_user` from a public method effectively make `create_guest_user` a public method? – settheline Dec 16 '13 at 22:11
  • No. How else would you call a private method if you didn't call it from a public method?? A method is private so that it can't be called from outside it's class. This is referencing Java, but it's still helpful.http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private – Catfish Dec 16 '13 at 22:16
  • Um...not sure as I'm fairly new to rails and programming in general. I only have experience calling private methods in before_filters and the like. I don't know if those qualify as "public methods", hence my question. – settheline Dec 16 '13 at 22:20