0

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

<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

结尾

4

3 回答 3

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。你这样做就解决了

于 2012-07-25T21:08:33.723 回答
1

尝试:

redirect_to user_car_path(@user, @car)

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

于 2012-07-25T20:58:25.073 回答
0

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

于 2012-07-25T20:41:17.463 回答