2012-07-25 109 views
3

我只有2周学习Ruby on Rails的,在我的应用程序的用户可以注册自己的汽车,从他们的个人资料(下面的代码)发送到注册汽车的应用页面,路由错误的路由匹配{:动作=>“秀”

<div class="container"> 
     <fieldset> 
      <h1><%= @user.email %></h1> 
      <br> 
      <h2>now you are able to...</h2> 
      <br> 
      <ul> 
       <li> 
        <strong>new car registration: </strong> 
        <%= link_to "new car", new_user_car_path(current_user)%> 
       </li> 
      </ul> 
     </fieldset> 
    </div> 

它的工作之前,但我不知道我这样做,现在它显示此:

Routing Error 

No route matches {:action=>"show", :user_id=>#<User id: 27, email: "[email protected]", encrypted_password: "$2a$10$EZtvPWiXgMfUlAqvuvGAzODMaas/y4rGkJPKJtg4PnC6...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-07-24 19:07:54", last_sign_in_at: "2012-07-24 19:07:54", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: nil, created_at: "2012-07-24 19:07:54", updated_at: "2012-07-24 19:07:54">, :controller=>"cars"} 

Try running rake routes for more information on available routes. 

也是我把我的carsController

class CarsController < ApplicationController 
    def new 
    @car = Car.new 
    end 

    def create 
    @car = current_user.Car.new(params[:car]) 
    if @car.save 
     flash[:notice] = "new car created success" 
     #redirect_to current_user, :flash => { :success => "car created!" } 
    else 
     #redirect_to new_user_car_path, 
     flash[:notice] = "sorry try again" 
    end 
    end 

    def index 
    @car=Car.all 
    end 

    def show 
    @car = current_user.car.find(params[:id]) 
    #@car = Car.find(params[:id]) 
    #redirect_to @user 
    end 
end 

和我的routes.rb

Estaciones::Application.routes.draw do 
root :to => "static_pages#home" 
match '/contact', :to=>'static_pages#contact' 
match '/about', :to=>'static_pages#about' 
devise_for :users 
resources :users do 
resources :cars 
end 

我耙路线:

root  /          static_pages#home 
      contact  /contact(.:format)      static_pages#contact 
       about  /about(.:format)      static_pages#about 
    new_user_session GET /users/sign_in(.:format)    devise/sessions#new 
     user_session POST /users/sign_in(.:format)     devise/sessions#create 
destroy_user_session DELETE /users/sign_out(.:format)    devise/sessions#destroy 
     user_password POST /users/password(.:format)    devise/passwords#create 
    new_user_password GET /users/password/new(.:format)   devise/passwords#new 
    edit_user_password GET /users/password/edit(.:format)   devise/passwords#edit 
        PUT /users/password(.:format)    devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)     devise/registrations#cancel 
    user_registration POST /users(.:format)      devise/registrations#create 
new_user_registration GET /users/sign_up(.:format)    devise/registrations#new 
edit_user_registration GET /users/edit(.:format)     devise/registrations#edit 
        PUT /users(.:format)      devise/registrations#update 
        DELETE /users(.:format)      devise/registrations#destroy 
      user_cars GET /users/:user_id/cars(.:format)   cars#index 
        POST /users/:user_id/cars(.:format)   cars#create 
     new_user_car GET /users/:user_id/cars/new(.:format)  cars#new 
     edit_user_car GET /users/:user_id/cars/:id/edit(.:format) cars#edit 
      user_car GET /users/:user_id/cars/:id(.:format)  cars#show 
        PUT /users/:user_id/cars/:id(.:format)  cars#update 
        DELETE /users/:user_id/cars/:id(.:format)  cars#destroy 
       users GET /users(.:format)      users#index 
        POST /users(.:format)      users#create 
      new_user GET /users/new(.:format)     users#new 
      edit_user GET /users/:id/edit(.:format)    users#edit 
       user GET /users/:id(.:format)     users#show 
        PUT /users/:id(.:format)     users#update 
        DELETE /users/:id(.:format)     users#destroy 

这是新车

<div class="container"> 
<h2>new car registration</h2> 

<%= form_for(:users, :url => user_car_path(current_user)) do |f| %> 
    <div><%= f.label :brand %><br /> 
    <%= f.text_field :brand %></div> 

    <div><%= f.label :color %><br /> 
    <%= f.text_field :color %></div> 

    <div><%= f.label :model %><br /> 
    <%= f.text_field :model %></div> 

    <div><%= f.label :year %><br /> 
    <%= f.text_field :year %></div> 

    <div><%= f.submit "new car",:class => "btn btn-primary" %></div> 
<% end %> 

+0

就像它说的那样,'rake routes'以洞察的方式提供了什么? – tadman 2012-07-25 16:02:42

+0

您能否按照建议提供耙路线的输出? – bento 2012-07-25 16:04:38

+0

有我的路线 – Asantoya17 2012-07-25 16:06:45

回答

4
<%= link_to "new car", new_user_car_path(current_user)%> 

应该给你的路线

 new_user_car GET /users/:user_id/cars/new(.:format)  cars#new 

是确定但错误说一辆车#show(不是新的!)不见了,所以也许找那个。

当错误究竟抛出?

增加:现在你已经张贴的形式,

我认为产生错误的行是

<%= form_for(:users, :url => user_car_path(current_user)) do |f| %> 

因为user_car_path需要既是用户,又一辆汽车 - 所以你需要

user_car_path(current_user,@car) 

我用刚刚这在我的表格:

<%= form_for ([@user,@car]) do |f| %> 

但底线是,每次你引用一辆汽车时,你还需要封闭的用户参考。

+0

错误发生时我试图创建一个新车(从链接必须通过新车形式) – Asantoya17 2012-07-25 16:35:11

+0

我无法修复它.. – Asantoya17 2012-07-25 16:52:28

+0

然后发布您的new.h车的tml.erb和_form.html.erb,它必须是那里的路径帮手。 – bento 2012-07-25 16:59:28

1

我不的new.html.erb不知道它是否会产生问题,但为什么你在这里通过current_user

<%= link_to "new car", new_user_car_path(current_user)%> 

在你的创作行为,您已经获得当前用户:

@car = current_user.Car.new(params[:car]) 
在你的表演动作

还你car,而不是Car

def show 
    @car = current_user.car.find(params[:id]) 

编辑 - 总部设在你的路线,我可以看到你有一个嵌套的资源:

您的汽车控制器应该是:

class CarsController < ApplicationController 
def new 
    @user = User.find(params[:user_id]) 
    @car = @user.cars.build 
end 

def create 
    @user = User.find(params[:user_id]) 
    @car = @user.cars.build(params[:car]) 
    if @car.save 
    flash[:notice] = "new car created success" 
    #redirect_to current_user, :flash => { :success => "car created!" } 
    else 
    #redirect_to new_user_car_path, 
    flash[:notice] = "sorry try again" 
    end 
end 

def index 
    @car=Car.all 
end 
... 

而且创造了新车的链接是new_user_car_path

+0

如果我删除该行另存为另一个错误: 路由错误没有路由匹配{:action =>“new”, :控制器=>“汽车”) – Asantoya17 2012-07-25 16:14:32

+0

任何想法,我试过你张贴,但仍然没有 – Asantoya17 2012-07-25 16:19:18

+0

我已编辑我的答案为你.. – gabrielhilal 2012-07-25 16:25:18

1

对于它的价值,我遇到了一个与OP描述的问题非常相似的问题:我的控制器的“索引”操作起作用,但“新”操作抛出了相同的异常(“显示”情况并非如此)。事实证明,Rails在我的模型名称的复数形式中存在一些问题(以'y'结尾)。

我没有打破惯例,而是选择了更简单的多元化的另一个模型名称,事情都很好。

相关问题