0
正如标题所说我想列出一个特定用户的所有汽车,还每节车厢有两个选项显示和更新列表中的所有用户对象
,你在图片中看到,我的问题是,当我想显示或编辑选定的汽车,它的路线(看图像的左下角),它采取所有汽车的所有ID,例如用户/“ID”/汽车/“ID1”/“ID2”,而不是采取具体车ID:用户/ “ID”/汽车/ “ID1”/ 这里是TE index.html.erb文件:
<div class="container">
<h1>Listing Cars</h1>
<table class="table table-condensed">
<tr>
<th>Brand</th>
<th>Color</th>
<th>Model</th>
<th>Year</th>
<th></th>
<th></th>
</tr>
<% @car.each do |car| %>
<tr>
<td><%= car.brand %></td>
<td><%= car.color %></td>
<td><%= car.model %></td>
<td><%= car.year %></td>
<td><%= link_to 'Show', user_car_path(@user,@car) %></td>
<td><%= link_to 'Edit', edit_user_car_path(@user, @car) %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New car', new_user_car_path, :class => "btn btn-primary" %>
</div>
以及是否需要汽车控制器:
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
redirect_to user_car_path(@user, @car), :flash => { :notice => " car created!" }
else
redirect_to new_user_car_path ,:flash => { :notice => " sorry try again :(" }
end
end
def show
@user = User.find(params[:user_id])
@car = @user.cars.find(params[:id])
#redirect_to user_car_path(@user, @car)
end
def index
@user = User.find(params[:user_id])
@car = @user.cars.all
end
def edit
@user = User.find(params[:user_id])
@car = @user.cars.find(params[:id])
#redirect_to user_car_path(@user, @car)
end
def update
@user = User.find(params[:user_id])
@car = @user.cars.find(params[:id])
if @car.update_attributes(params[:car])
redirect_to user_cars_path, :flash => { :notice => " Car Updated!" }
else
render 'edit'
end
end
end
非常感谢我修复了它 – Asantoya17 2012-07-26 15:16:40