简体   繁体   English

路由错误没有路由匹配

[英]Routing Error No route matches

I'm sorry to bother again, but these routes drive me crazy, on my app later that the user create the new car it must show the car created when the user push "new car". 我很抱歉再次打扰,但是这些路线让我发疯,在我的应用程序之后用户创建新车时必须显示用户按“新车”时创建的汽车。

<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>

ok my Carscontroller is this: 好吧我的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

so, when the user push new car it shows 所以,当用户推新车时,它会显示出来

Routing Error

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

instead of showing the new car 而不是展示新车

here is my routes.rb 这是我的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 结束

Your show action should be something like this: 你的节目动作应该是这样的:

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

Let me know if this doesnt work. 如果这不起作用,请告诉我。

EDIT: There is an error in your routes.rb, its missing additional keyword end . 编辑:你的routes.rb中有一个错误,它缺少额外的关键字end It will be solved if you do that 如果你这样做,它将被解决

Can you post your config/routes.rb file? 你可以发布你的config / routes.rb文件吗? I would almost think you don't have the nested routes setup in there correctly, but seeing the file would help. 我几乎认为你没有正确设置嵌套路由,但看到文件会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM