2016-07-20 40 views
0

我一直在尋找正確的答案,但沒有成功。所以我有一個列表,人們可以在列表中籤名並選擇一個特定的理髮師。我想在我的視圖/ barbers/show.html.erb 中顯示特定理髮師下的所有名稱我是新手,因此我非常感謝任何人的幫助。謝謝:)如何在Rails中列出來自特定類別的所有名稱?

下面是我列出的控制器:

class ListsController < ApplicationController 
    before_action :find_list, only: [:show, :edit, :update, :destroy] 
    def index 
    @list = List.all.order("created_at asc") 
    end 

    def new 
    @list = List.new 
    end 

    def create 
    @list = List.new list_params 

    if @list.save 
     redirect_to root_path, notice: "#{@list.name}, You have been added to the List!" 
    else 
     render 'new', notice: "Oh No! Not Saved!" 
    end 
    end 

    def show 

    end 

    def edit 

    end 

    def update 
    if @list.update list_params 
     redirect_to @list, notice: "#{@list.name}, has been updated!" 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    @list.destroy 
    redirect_to root_path, notice: "#{@list.name}, has been deleted!" 
    end 

    private 

    def list_params 
    params.require(:list).permit(:name, :barber_id) 
    end 

    def find_list 
    @list = List.find(params[:id]) 
    end 

end 

理髮師控制器:

class BarbersController < ApplicationController 
    before_action :find_barber, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! 
    def index 
    @barber = Barber.all.order("created_at desc") 
    end 

    def new 
    @barber = Barber.new 
    end 

    def create 
    @barber = Barber.new barber_params 

    if @barber.save 
     redirect_to @barber, notice: "#{@barber.name} has been saved!" 
    else 
     render 'new', notice: "Oh No!#{@barber.name} couldn't save!" 
    end 
    end 

    def show 

    end 

    def edit 

    end 

    def update 
    if @barber.update barber_params 
     redirect_to @barber, notice: "#{@barber.name} has been updated" 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    @barber.destroy 
    redirect_to barbers_path 
    end 

    private 

    def barber_params 
    params.require(:barber).permit(:name, :barber_id) 
    end 

    def find_barber 
    @barber = Barber.find(params[:id]) 
    end 

end 

理髮師型號:

class Barber < ActiveRecord::Base 
    has_many :lists 
end 

列表型號:

class List < ActiveRecord::Base 
    has_one :barber 
    belongs_to :barber 
end 

Show.html.erb:

<div class="container"> 
    <div class="row"> 
    <div class="col-md-10 list center-block"> 
     <div class="col-md-6 center-block names panel-default panel"> 
     <h1><%= @barber.name %></h1> 
     <hr> 
     </div> 
     <div class="col-md-2 center-block"> 
     <%= link_to "edit", edit_barber_path(@barber), class: "btn btn-default btn-edit"%> 
     <%= link_to "Delete", barber_path(@barber), method: :delete, data: {confirm: "Are you sure you want to delete this Barber?"}, class: "btn btn-default btn-delete"%> 
     </div> 
    </div> 
    </div> 
</div> 

這裏是我的形式,以防萬一:

<%= form_for @list do |f| %> 
    <% if @list.errors.any?%> 
    <h2><%= pluralize(@list.errors.count, "error") %> prevented this list from saving:</h2> 
    <ul> 
     <% @list.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <%end%> 
    </ul> 
    <%end%> 

    <div class="form-group "> 
    <%= f.label :name %> 
    <%= f.text_field :name, {class: 'form-control'} %> 
    </div> 

    <div class="form-group"> 
    <%= f.label "Choose a Barber" %> 

    <div class="classic-select"> 
     <%= f.collection_select :barber_id, Barber.all, :id, :name %> 
    </div> 

    </div> 
    <br> 
    <button type="submit" class="btn-add-to-list">Submit</button> 
<%end%> 
+0

'Barber.find(params [:id).lists'? – potashin

+0

我試過但沒有工作。 @potashin還有更多建議嗎? – Bryan

回答

1

列表型號:

class List < ActiveRecord::Base 
    # REMOVE THIS: has_one :barber 
    belongs_to :barber 
end 

Show.html.erb :

<div class="container"> 
    <div class="row"> 
    <div class="col-md-10 list center-block"> 
     <div class="col-md-6 center-block names panel-default panel"> 
     <h1><%= @barber.name %></h1> 
     <% for list in @barber.lists %> 
      <p><%= list.name %></p> 
     <% end %> 
     <hr> 
     </div> 
     <div class="col-md-2 center-block"> 
     <%= link_to "edit", edit_barber_path(@barber), class: "btn btn-default btn-edit"%> 
     <%= link_to "Delete", barber_path(@barber), method: :delete, data: {confirm: "Are you sure you want to delete this Barber?"}, class: "btn btn-default btn-delete"%> 
     </div> 
    </div> 
    </div> 
</div> 
+0

謝謝!有效!!我現在如何在我的索引中執行此操作? @blnc – Bryan

+0

nvm我解決了它!非常感謝! – Bryan

+0

剛剛看到這個,很高興它幫助你的歡迎! – blnc

相關問題