我只有2周时间在轨道上学习红宝石,在我的应用程序中,用户可以通过他们的个人资料(下面的代码)注册他们的汽车,将应用程序发送到注册汽车页面,
<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: "armando.santoya@hotmail.com", 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
这是新车的new.html.erb
<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 :(得分: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| %>
但最重要的是,每当您引用汽车时,您还需要附上用户参考。
答案 1 :(得分:1)
我不知道它是否会产生问题,但你为什么要通过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
答案 2 :(得分:1)
对于它的价值,我遇到的问题与OP描述的问题非常类似:我的控制器的“索引”操作有效,但“新”操作引发了相同的异常(“显示”路线丢失,这是不是这样)。事实证明,Rails在我的模型名称的复数化方面存在一些问题(以'y'结尾)。
与其对抗惯例,我最终选择了另一个模型名称,更简单的复数,事情还不错。