jamal
September 9, 2010, 11:24am
#1
Hello,
I will try to explain it step by step
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
//James
jamal
September 9, 2010, 11:26am
#2
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 %>
jamal
September 9, 2010, 1:57pm
#3
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.
jamal
September 9, 2010, 1:58pm
#4
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 ])
jamal
September 9, 2010, 2:43pm
#5
did you put the = after <% ?
jamal
September 9, 2010, 2:00pm
#6
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?
jamal
September 9, 2010, 4:49pm
#7
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?
jamal
September 9, 2010, 5:26pm
#8
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
Thanx for help
jamal
September 9, 2010, 5:25pm
#9
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 %>
jamal
September 9, 2010, 5:27pm
#10
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
jamal
September 9, 2010, 5:29pm
#12
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
jamal
February 8, 2011, 8:17pm
#13
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!
jamal
February 8, 2011, 8:26pm
#14
I switched to simple_form.
It’s simple and better
jamal
February 8, 2011, 8:47pm
#15
Try
semantic_form_for [:admin, @user ] do |f|
[… etc]
You shouldn’t need to supply a path at all in this case.
Phil
jamal
February 8, 2011, 9:21pm
#16
I thought that was the normal way to do it… 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
jamal
February 8, 2011, 8:51pm
#17
Hehe – that’s awesome … the [ :admin, @user ] trick worked in
formtastic and in simple_form …
Thanks!
Blair
jamal
February 9, 2011, 10:03am
#18
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