Ruby on Railsを学習するのに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.
また、私は自分の車を置きますController
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 %>