Rails 3 Route错误 - “No Route Matches”

时间:2011-08-31 02:20:57

标签: ruby-on-rails-3 routes

我一直在阅读this resource以及this post以尝试更多地了解路线(目前正在学习编程/ Rails),但我想知道如何解决我遇到的错误,No route matches {:controller=>"profiles", :action=>"show"}

我使用嵌套模型表单通过Rails 3注册过程得到了错误。注册过程如下:

user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.save
user.save

注册过程从主页开始,格式如下:

<%= form_for :user, :url => signup_path, :html => {:id => 'homepage'} do |f| %>
  <div>
  ...
  </div>
  <%= f.fields_for :profile do |f| %>
  <% end %>
<% end %>

然后该过程将填写个人资料,然后在此表单完成后重定向到新用户的个人资料:

<%= form_for :profile, :html => { :multipart => true } do |f| %>
  <div>
  ...
  </div>
  <%= f.fields_for :user do |f| %>
  <% end %>
<% end %>

我在各自的模特中都有accepts_nested_attributes_for :user and :profile

我的rails服务器它给了我更多细节:

ActionController::RoutingError (No route matches {:controller=>"profile.save",     :action=>"show"}):
  app/controllers/profiles_controller.rb:15:in `create'

所以在我的ProfilesController中'create':

def create
  @profile = Profile.new(params[:profile])
  if @profile.save
    redirect_to profile_path, :notice => 'User successfully added.'
  else
    render :action => 'new'
  end
end

似乎清楚问题出在profile_path,所以我的Routes.rb:

post "/signup" => "profiles#create", :as => "signup"
match "skip/signup", :to => "info#signupskip"
match "skip/profiles/new", :to => "profiles#newskip"
root :to => "users#create"

任何人都可以帮助了解我在Routes.rb文件中出错/丢失的内容吗?

1 个答案:

答案 0 :(得分:1)

重定向路径应包含要重定向到的特定配置文件:

if @profile.save
   redirect_to profile_path(@profile), :notice => 'User successfully added.'
else
.....

此外,路线应包括以下一行:

get "/profiles/:id" => "profiles#show", as => "profile"