2017-03-16 74 views
-1

这是我的车的控制器.. 如何在指数方法加那么,我们将在我们的索引页面,我想在我的车索引中显示的用户名

class CarsController < ApplicationController 
    before_action :set_car, only: [:show, :edit, :update, :destroy] 
    before_filter :authenticate_user! , except: [:index,:show] 

    def index 
    @cars = Car.all 
    end 

    def show 
    end 

    def new 
    @car = current_user.cars.build 
    end 

    def edit 
    end 

    def create 
    @car = current_user.cars.build(car_params) 

    respond_to do |format| 
    if @car.save 
     format.html { redirect_to @car, notice: 'Car was successfully  created.' } 
     format.json { render :show, status: :created, location: @car } 
    else 
     format.html { render :new } 
     format.json { render json: @car.errors, status::unprocessable_entity } 
    end 
    end 
end  

,这让用户的电子邮件是我index.what在索引添加到显示在所有的汽车用户的电子邮件和所有ussers应该abale看到USEREMAIL

<%[email protected]%> 
<div class="row" > 
    <% @cars.each do |car| %> 
    <div class="col-sm-8" > 
     <div class="polaroid center-block" > 
     <%=link_to (image_tag car.avatar.url(:medium)),car %> 
     <div style="float:right"> 
     <p><%= link_to edit_car_path(car) do %> 
      <i class = "glyphicon glyphicon-edit "></i> 
     <%end %></p> 
     | 
     <p><%= link_to car, method: :delete, data: { confirm 'Are you  sure?' } do %> 
     <i class = "glyphicon glyphicon-trash"></i> 
     <%end%></p></div> 
     </div><br> 
    </div> 
    <% end %> 

我想显示USEREMAIL的用户谁张贴的汽车在我的索引页。我应该在控制器中添加什么,以及我的索引如何...

回答

0

如果你的车有关联belongs_to :user然后添加到您的控制器:@car = Car.includes(:user).all

在索引页面,您可以添加<%= car.user.usermail %>

+0

它不工作,因为我在高职像我的索引方法添加分页.. @ cars = Car.paginate(:page => params [:page],:per_page => 5) – aaquib

相关问题