路由错误没有路由匹配

时间:2012-07-25 20:36:45

标签: ruby-on-rails routing controller

我很抱歉再次打扰,但这些路线让我发疯,在我的应用程序之后用户创建新车时必须显示用户推动“新车”时创建的汽车。

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

    <%= form_for ([@user,@car]) 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 %>
    <br />
    <%= link_to "Back", current_user,:class => "btn btn-primary"  %>
  </div>

好吧,我的Carscontroller是这样的:

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

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

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

def index
    @car=Car.all
    respond_to do |format|
    format.html  #index.html.erb
    format.json  { render :json => @car }
  end
 end
end

所以,当用户推新车时显示

Routing Error

No route matches {:action=>"show", :controller=>"cars"}
Try running rake routes for more information on available routes.

而不是展示新车

这是我的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

3 个答案:

答案 0 :(得分:1)

尝试:

redirect_to user_car_path(@user, @car)

http://guides.rubyonrails.org/routing.html#nested-resources

答案 1 :(得分:1)

你的节目动作应该是这样的:

 def show
    @user = User.find(params[:user_id])     
    @car = @user.cars.find(params[:id]) 
    redirect_to user_car_path(@user, @car)  
 end

如果这不起作用,请告诉我。

编辑:您的routes.rb中存在错误,其中缺少其他关键字end。如果你那样做就会解决

答案 2 :(得分:0)

你可以发布你的config / routes.rb文件吗?我几乎认为你没有正确设置嵌套路由,但看到文件会有所帮助。