【问题标题】:Rails 3 Route error - "No Route Matches"Rails 3 路线错误 - “没有路线匹配”
【发布时间】:2011-08-31 02:20:57
【问题描述】:

我一直在阅读 this resourcethis post 以尝试更多地了解 Routes(目前正在通过实践学习编程/Rails),但我想知道如何解决我遇到的错误,即 @ 987654323@。

我在使用嵌套模型表单完成 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 中的“创建”中:

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 文件中做错了什么/遗漏了什么?

【问题讨论】:

    标签: ruby-on-rails-3 routes


    【解决方案1】:

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

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

    路线也应该包括这一行:

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

    【讨论】:

    • 有“get”但忘记添加特定的配置文件。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多