从呈现的成员创建复选框表单以发送电子邮件 - Ruby on Rails

时间:2012-04-10 21:59:44

标签: ruby-on-rails ruby-on-rails-3 forms

我有一个团队成员页面,它通过与大学体育俱乐部相关的用户,我试图让它在每个成员旁边生成一个复选框,以便委员会成员可以简单地选择几个成员和按下提交按钮后,系统将向他们发送电子邮件。

我试过<%= check_box_tag(<%= render @users %>) %>但没有运气

show_selection.html.rb - 这是呈现用户的页面

<% if signed_in? %>
    <% if current_user.captain? %>
        Welcome, select members to send a message to below

    <table summary="Team members">
      <tr>
        <td class="main">
          <% unless @users.empty? %>
            <ul class="users">
              <%= render @users %>
            </ul>
            <%= will_paginate @users %>
          <% end %>
        </td>       
      </tr>
    </table>

    <br>
    <br>

<% else %>
    <h1>An error has occured</h1>
    <br>
    <p>It looks like you're trying to access a restricted page <%= link_to "Please Click Here", '/' %> </p>
    <% end %>
<% else %>
    An error has occured
    <br>
    <p>It looks like you're trying to access a restricted page <%= link_to "Please Click Here", '/' %> </p>
<% end %>

users_contorller.rb

class UsersController < ApplicationController
  before_filter :authenticate, :only => [:index, :edit, :update, :destroy]
  before_filter :correct_user, :only => [:edit, :update]
  before_filter :admin_user,   :only => :destroy

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(:page => params[:page])
    @title = @user.name
    @admins = User.where(:admin => "t")
    @captains = User.where(:captain => "t")
    @clubs = User.where(:captain => "t")
  end

  def new
    @user = User.new
    @title = "Sign up"
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to University Sports!"
      redirect_to @user
      UserMailer.welcome_email(@user).deliver
    else
      @title = "Sign up"
      render 'new'
    end
  end

  def edit
    @title = "Edit user"
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated."
      redirect_to @user
    else
      @title = "Edit user"
      render 'edit'
    end
  end

  def index
    @users = User.paginate(:page => params[:page])
  end

  #def admins
  #  @users = User.admins
  #  render "users/index"
  #end

  def admins
    @admins=User.where(:admin => "t")
  end

  def captains
    @captains=User.where(:captain => "t")
  end

  def clubs
    @clubs=User.where(:captain => "t")
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User destroyed."
    redirect_to users_path
  end

  def following
    @title = "Following"
    @user = User.find(params[:id])
    @users = @user.following.paginate(:page => params[:page])
    render 'show_follow'
  end

  def followers
    @title = "Followers"
    @user = User.find(params[:id])
    @users = @user.followers.paginate(:page => params[:page])
    render 'show_follow'
  end

  def selection
    @title = "Selection"
    @user = User.find(params[:id])
    @users = @user.followers.paginate(:page => params[:page])
    render 'show_selection'
  end 

  private

    def authenticate
      deny_access unless signed_in?
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end

    def admin_user
      redirect_to(root_path) unless current_user.admin?
    end

end

1 个答案:

答案 0 :(得分:0)

首先,您需要将其包装在一个表单中,以处理所选成员提交给电子邮件的问题。表格应张贴到邮件控制器或处理所选成员的动作以进行电子邮件发送。

 <%= render @users %>

这将为@users中的每个对象呈现部分。您应该在此部分内添加复选框,以便为每个成员呈现它。

你的部分

会有类似的东西
<%= check_box_tag "user_ids[]", user.id %>

Ryan有一个railscast,它将指出你如何实施的正确方向。这是关于更新记录,但您可以修改代码以使用您的方案。

http://railscasts.com/episodes/52-update-through-checkboxes