很抱歉再次打扰,但这些路线让我发疯,稍后在我的应用程序上,用户创建新车时,它必须显示用户推送“新车”时创建的汽车。
<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.
而不是展示新车
这是我的路线.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
结尾