Formtastic issue

Hello,

I will try to explain it step by step :slight_smile:

I just created a new rails 3 app, then I created a new controller…

rails generate controller admin::users

I didn’t forget to add the resources in the routes.rb file like this.

namespace :admin do
resources :users
end

Now I try to use formtastic to create the form but I get erorr that my
users_path doesn’t exist?

undefined method `users_path’ for #<#Class:0x104169a40:0x104167df8>

…even though I have created resources for users.

Can someone explain to me what’s happening.

Thanks for any help :smiley:

//James

Just in case anyone asked for the code.

It is simple:


Controller

class Admin::UsersController < ApplicationController
def new
@user = User.new
end
end


View

<% semantic_form_for @user do |form| %>
<%= form.inputs %>
<%= form.buttons %>
<% end %>


polymorphic_path([:admin,@user])

you can pass just the array [:admin,@user]

since you nested your route all you helper methods are now nested and
passing the usual wont work anymore

from now on is

show admin_user_path(@user) no more using just @user , you
can
also pass the array [:admin, @user]
index admin_users_path
new new_admin_user_path
edit edit_admin_user_path(@user)
destroy destroy_admin_user_path(@user)
create [:admin, User.new]
update [:admin, @user]

confirm all of the nested routes by doing rake routes , when i doubt use
polymorphic_path([:admin,@user]) this method will create the correct
path.

forgot to say that with polymorphic_path([:admin,@user]) you can pass
the
action as a parameter to the array , like this

polymorphic_path([:admin,:new,@user])

did you put the = after <% ?

Thanks for answer radhames,

I just tried it and it works.

But now it doesn’t print out the input fields, only the button on the
bottom?

radhames brito wrote:

did you put the = after <% ?

Yes.

<% semantic_form_for polymorphic_path([:admin, @user]) do |form| %>
<%= form.inputs %>
<%= form.buttons %>
<% end %>

Another issue.

<form accept-charset="UTF-8" action="/admin/users/new"

It’s pointing on the wrong action? it should point to create POST,
right?

radhames brito wrote:

try this

<% semantic_form_for polymorphic_path([:admin, @user]) do |form| %>
<% form.inputs do %>
<%= form.input :name %>
<%= form.input :last_name %>
<%end%>
<% form.buttons do %>
<%=f.commit_button “OMG”%
<%end%>
<% end %>

I just did [:admin, @user] and remove polymorphic and then it point
correct :smiley:

Thanx for help

try this

<% semantic_form_for polymorphic_path([:admin, @user]) do |form| %>
<% form.inputs do %>
<%= form.input :name %>
<%= form.input :last_name %>
<%end%>
<% form.buttons do %>
<%=f.commit_button “OMG”%
<%end%>
<% end %>

You nested the route and the controller when you did this
Admin::UsersController so action="/admin/users/new" is correct in fact
your
views should be in views/admin/user

class Admin::UsersController < ApplicationController
def new
@user = User.new
end
end

you are nesting

oh, good

if nothing show after this then check if you are creating and instance
of
user in the user controller

def new
@user = User.new
end

radhames, I’m having the exact same issue here. Here’s my code:

<%= semantic_form_for polymorphic_path([:admin, @user]) do |f| %>
<%= f.inputs do %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :username %>
<%= f.input :email %>
<%= f.input :password, :as => :password %>
<%= f.input :role, :as => :select, :collection => User.roles.collect
{|x| x.to_s } %>
<% end %>
<%= f.buttons %>
<% end %>

No matter what I do – the action that sematic_form_for generates is:

/admin/users/new

instead of /admin/users

I even tried to add the absolute path

<%= semantic_form_for admin_users_path do |f| %>

and still the action for the form came out to be /admin/users/new

Let me know if there’s something I can do to get this working properly.

Thanks!

I switched to simple_form.

It’s simple and better :smiley:

Try

semantic_form_for [:admin, @user] do |f|
[… etc]

You shouldn’t need to supply a path at all in this case.

Phil

I thought that was the normal way to do it… :slight_smile: I never put a url
into
form_for unless it can’t correctly guess it from the object or object
array… Glad it solved the problem.

Phil

Hehe – that’s awesome … the [ :admin, @user ] trick worked in
formtastic and in simple_form :slight_smile:

Thanks!

Blair

On Feb 8, 8:44pm, Phil C. [email protected] wrote:

Try

semantic_form_for [:admin, @user] do |f|
[… etc]

semantic_form_for @user, :url => polymorphic_path( [:admin, @user] )
do |f|

should also works fine.

Robert Pankowecki