Question

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!

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top