2016-06-09 40 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 –

相关问题