2016-06-09 33 views
0

我有點新的Ruby on Rails和StackOverflow。我有一個Rails應用程序,人們可以在名單上簽名。但我想發送電子郵件通知給名單中第二位的人。我怎樣才能做到這一點?我需要一個if語句嗎?我如何讓鐵軌知道誰是第二?如果客戶端在Ruby on 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 

這是我的形式

<%= 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" %> 
    <!-- :include_blank => true 
     insert this to have the option of leaving it blank 
    --> 
    <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%> 

這裏是我的index.html.erb

<div class="container"> 
    <div class="row"> 
    <div class="col-md-10 list center-block"> 
     <% @list.each do |list| %> 
     <div class="col-md-6 names panel-default panel"> 
      <h1><%= link_to list.name, list %></h1> 
      <p><strong>Barber:</strong> <%= list.barber.name %></p> 
      <hr> 
     </div> 
     <%end%> 
     <div class="col-md-2 center-block"> 
     <%= link_to "Add Me to the List", new_list_path, class: "btn-add btn"%> 
     </div> 
    </div> 
    </div> 
</div> 

回答

0

喲可以獲得關於你的第二個index with:

@list = List.all.order("created_at asc") 
@second_on_list = @list.second 

或者

@list = List.all.order("created_at asc") 
@second_on_list = @list[1] 
+1

真棒傢伙!非常感謝!所以現在我只是在我的if語句中使用變量來發送我的電子郵件 – Bryan

+0

歡迎您:D –

相關問題