无法将信息添加到数组中

时间:2014-12-24 14:54:13

标签: ruby-on-rails arrays ruby-on-rails-4 model controller

在我的rails应用程序中,我有一个模型/控制器'Group',其中包含参数push_arrayuser_array。 push_array用于通过表单将信息保存到user_array。出于某种原因,在将信息输入表单后,都没有保存推送阵列或用户阵列。我怎么能让这个工作?

组控制器

class GroupsController < ApplicationController
  before_action :set_group, only: [:show, :edit, :update, :destroy, :add]
  before_filter :authenticate_user!, :only => [:show, :create, :new, :edit, :update, :destroy]
  before_filter :check_groupprivileges!, :only => [:edit, :destroy]

  respond_to :html

  def index
    @groups = Group.limit(3).order("id").all
    respond_with(@groups)
  end

  def show
    @posts = @group.posts.order("created_at DESC")
    @group.user_array << @group.push_array
    @group.save
    @group_id = @group.id
    respond_with(@group)
  end

  def add
    @group.user_array << @group.push_array
    @group.save
    respond_with(@group)
  end

  def new
    @group = Group.new
    respond_with(@group)
  end

  def edit
  end

非常感谢任何帮助。

  def create
    @group = Group.new(group_params)
    @group.save
    respond_with(@group)
  end

  def update
    @group.update(group_params)
    respond_with(@group)
  end

  def destroy
    @group = Group.find(params[:id])
    @group.destroy
    respond_with(@group)
  end

  private
    def set_group
      @group = Group.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:id, :group, :group_id, :user, :poster, :posterid)
    end

    def group_params
      params.require(:group).permit(:name, :description, :motto, :usercount, :group, :id, :groupid, :user_array, :user_ids, :usersid, :creatorid, :push_array)
    end
end

group /:id / add View

<%= form_tag(@group, :method => 'get') do %>
  <strong><%= text_field_tag :push_array, params[:push_array] %><%= submit_tag "Add User" %></strong></br>
  <%= @group.user_array %>
<% end %>

1 个答案:

答案 0 :(得分:0)

表单发送的参数位于param变量中,型号为键,请尝试使用@group.user_array << params[:group][:push_array]