0

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

我试过<%= 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
4

1 回答 1

0

首先,您需要将其包装在一个表单中,以处理将所选成员提交到电子邮件。表单应发布到邮件控制器或处理选定成员以发送电子邮件的操作。

 <%= render @users %>

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

你的部分中会有这样的东西

<%= check_box_tag "user_ids[]", user.id %>

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

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

于 2012-04-11T02:15:58.870 回答