3

我只有 2 周的时间学习 ruby​​ on rails,在我的应用程序中,用户可以注册他们的汽车,从他们的个人资料(下面的代码)发送到注册汽车页面的应用程序,

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

我也把我的汽车控制器

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

4

3 回答 3

5
<%= 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| %>

但最重要的是,每次引用汽车时,还需要包含用户引用。

于 2012-07-25T16:30:29.553 回答
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

于 2012-07-25T16:10:05.960 回答
1

对于它的价值,我遇到了一个与 OP 描述的问题非常相似的问题:我的控制器的“索引”操作有效,但“新”操作引发了相同的异常(“显示”路由丢失,情况并非如此)。事实证明,Rails 对我的模型名称的复数形式(以“y”结尾)存在一些问题。

我最终选择了另一个具有更简单复数的模型名称,而不是与惯例作斗争,一切都很好。

于 2013-07-01T19:25:01.713 回答