Topic: Authlogic, formtastic and Rails 3

Hi,

I'm in the process of upgrading a rails app to rails 3. I ran into the "undefined method `to_key'" problem, which I solved with http://railsforum.com/viewtopic.php?id=39220

So now, I get the login form. But when I submit it, I get

No route matches "/user_sessions/%23%3CUserSession:0xb65e5cfc%3E"

I believe this one is caused by formtastic but I've no idea how to work around it.

Any pointer ?

My controller:

class UserSessionsController < ApplicationController

  def new
    @user_session = UserSession.new
  end
  
  def create
    @user_session = UserSession.new(params[:user_session])  
    if @user_session.save  
      flash[:notice] = "Welcome"  
      redirect_to root_url  
    else  
      render :action => 'new'  
    end  
  end
  
  def destroy  
    @user_session = UserSession.find  
    @user_session.destroy  
    flash[:notice] = "Bye."  
    redirect_to root_url  
  end
  
end

And my view:

<%= semantic_form_for @user_session do |f| %>
  <% f.inputs do %>
    <%= f.input :username %>
    <%= f.input :password %>
  <% end %>
  <%= f.buttons %>
<% end %>

Last edited by Kaloo (2011-04-18 17:12:24)

Re: Authlogic, formtastic and Rails 3

Just to clarify, you are actually getting the form to load, you're just not able to submit it -- is that right?

I've had issues with models where there are no database tables while using Formtastic, since this gem needs to inflect on the field/column types in order to figure out how to render the default inputs. There are workarounds for this, but it doesn't sound like your issue.

Can you give us the gem versions and so on that you are working with? Have you tried using Formtastic on any other models in the same project? I'm using formtastic on several rails 3 projects, so this has to be something project/model specific.

Another brief comment (that probably isn't related), but you should have

= f.inputs do

for rails 3 (this will be deprecated soon).

Chris

Re: Authlogic, formtastic and Rails 3

Yes, the form loads but the submit URL is wrong.
And yes, formtastic works for other models.

My gem versions :
formtastic (1.2.3)
authlogic (2.1.6)

Re: Authlogic, formtastic and Rails 3

I thought that I might have a project with a model using both ActiveRecordless (that is, a tableless model) and formtastic, and I do, but it looks like it is still a Rails 2 app. And sadly, it's not an app I'm interested in upgrading. Also, I think that all of my authorization projects on rails 3 are just using regular old forms, so that is also not of much help.

I know, formtastic kicks ass, but the obvious and easy solution is to just use regular forms yourself. You'll end up spending way less time typing those couple of extra lines than you will fussing with formtastic to get it to work right.

This definitely does look like a bug with formtastic though, so if you think you have time to figure out what the issue is (and want to so that you don't have to cow down to standard forms in the future), then go for it. The reason why I'm thinking that this is a bug with formtastic is that it should really be spitting out the correct path here, regardless of whether the model is actually active record or not. (Of course, it's not really designed to work on anything other than active_record models, so I'm sure it's not high priority for them).

What I would do is pull out ruby debug and throw in a breakpoint just before the call to formtastic_form_for. Then work your way down into the code and find out where it is creating the path. Once you are there, you should be able to figure out why it is throwing that extra junk on (which pretty clearly seems to be the object id of the new session your form was built off of). Then maybe you can fix it.

Another potentially simple solution is to try and override the faulty path that formtastic is giving you. You might be able to just pass in :path => "/user_sessions/". If not, look around to see if there is a way to specify this for regular forms -- formtastic just inherits from these, so if there is, it might be portable (assuming it doesn't try to override that with it's bug).

Chris

Re: Authlogic, formtastic and Rails 3

Hi,

:url => "/user_sessions/" did the trick.
It's described here : http://api.rubyonrails.org/classes/Acti … elper.html

So here is what my view looks like now :

<%= semantic_form_for @user_session, :url => "/user_sessions/" do |f| %>
  <%= f.inputs do %>
    <%= f.input :username %>
    <%= f.input :password %>
  <% end %>
  <%= f.buttons %>
<% end %>

Unfortunately, I don't have time to investigate further. I'm on a  tight schedule with this app and the client isn't likely to pay me overtime...

Re: Authlogic, formtastic and Rails 3

Cool -- I'm glad that worked.