开发者

Rails 3 Namespace Routing Error - Route exists but is not working

This has been fixed. I guess I had left :method => put in the form helper when I copied it from another view I had written. False alarm.

I have a route in my rails 3 application that shows up in 'rake routes' but ends up raising a no route matches error.

I am attempting to create a namespaced route for creating and adding new users under a company that the current_user manages. The namespace is 'company'. The controller that should handle this operation is the 'users_controller' and is located inside of the controllers->company folder.

My routes.rb file contains this code:

namespace :company do
  resources :users
  resources :manage, :only => [:show,:edit,:update]
end

devise_for :users

My users_controller.rb under the app->controllers->company folder contains this code:

def new
  @user = current_user.company.users.new
end

def create
  @user = current_user.company.users.create(params[:user])
  if @user.save
    flash[:notice] = "Successfully Added New User"
    redirect_to company_path(current_user.company.id)
  else
    flash[:warn] = "Correct Errors and Resubmit"
   # render :action => 'new'
  end
end

The view for the create action under app->views->company->users->create.html.erb contains:

<h2>Adding User for <%= current_user.company.name %></h2>

<%= form_for(@user, :url => company_users_path) do |f| %>


  <p><%= f.label :email %><br />
  <%= f.email_field :email %></p>

  <p><%= f.label :first_name %><br />
  <%= f.text_field :first_name %></p>

  <p><%= f.label :last_name %><br />
  <%= f.text_field :last_name %></p>

  <p><%= f.label :password, "New Password" %><br />
    <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation, "Confirm New Password" %><br />
    <%= f.password_field :password_confirmation %></p>

  <p><%= f.submit "Add User" %></p>
<% end %>

<%= link_to "Back", :back %>

And finally when I run the 'rake routes' command I get this:

company_users     GET    /company/users(.:format)           {:action=>"index",    :controller=>"company/users"}
                  POST   /company/users(.:format)           {:action=>"create", :controller=>"company/users"}
new_company_user  GET    /company/users/new(.:format)       {:action=>"new", :controller=>"company/users"}
edit_company_user GET    /company/users/:id/edit(.:format)  {:action=>"edit", :controller=>"company/users"}
company_user      GET    /company/users/:id(.:format)       {:action=>"show", :controller=>"company/users"}
                  PUT    /company/users/:id(.:format)       {:action=>"update", :controller=>"company/users"}
                  DELETE /company/users/:id(.:format)       {:action=>"destroy", :controller=>"company/users"}

The 'new' action renders just fine. The error which I receive after submitting the form is:

Routing Error

No route matches "/company/users

I am using the devise gem for authentication and can provide more details about my application if need be. So far everything else I have added works great. I am guessing it has something to do with how I am defining the namespace. I am still fairly new to rails and this is my first attempt at namespace routing.

I apologize if I provided too much information or too little. Any help is greatly apprec开发者_开发技巧iated.


Try to change the form_for to:

form_for([:company,@user])

It should work. See http://guides.rubyonrails.org/form_helpers.html#dealing-with-namespaces

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜