0

User我和Micropost( )有关系User has_many Micropost and Micropost belongs_to User

在模态中,我有一个带有select2框的表单。我想做的是,选择尽可能多的用户,然后从一个表单中向这些用户发布微博。我该怎么做。

基本上我想在一个表单提交的select2框中创建带有所选用户的user_ids的微帖子。

发布后的微博表(假设我选择了 4 个用户来发布我的消息)

micropost_id    micropost      user_id       posted_by_user_id
----------------------------------------------------------------------
1               Some Message       1         Current_user_id(say 5)   
2               Some Message       2         Current_user_id(say 5)    
3               Some Message       3         Current_user_id(say 5)   
4               Some Message       4         Current_user_id(say 5)  

形式

<form accept-charset="UTF-8" class="simple_form new_share" id="new_share" method="post">
<%= csrf_meta_tag %>
<div class="form-inputs">
    <select multiple id="UserSelect" style="width:100%">
        <%@users.each do |user| %>
            <option value="<%=user.id%>"><%=user.username%></option>
        <% end %>
    </select>             
    <div class="control-group text required">
        <div class="controls">
            <p> Enter your Custom Message </p>
            <textarea class="text required span4" id="review_text_area" 
                    name="review[review]" placeholder="Share" 
                    rows="3">
            </textarea>
        </div>
    </div> 
</div>
<div class="modal-footer">
    <button id="ShareModalSubmit" class="btn" type="submit" value="Share">Share</button>
    <button id="share-confirm-close" class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>                        
</div>

我不知道,如果我有道理..

这甚至可能吗?如果不是我如何实现这个功能?

整晚都在工作..
请多多包涵,我是新手。

4

1 回答 1

0

我设法解决它,这......不确定它是否是最干净的解决方案。如果有人知道任何干净的解决方案,请告诉我。

def create
respond_to do |format|
  format.js {
      if params[:Users_ids]  #Coming from select box
            univ_id = params[:univ_id]
            univ_name = params[:univ_name]

            params[:SelectedUsers_ids].each do |user_id|
              @user = User.find(user_id.to_i)
              params[:user_id] = user_id.to_i                                          
              @micropost = @user.microposts.build(params[:micropost])
              if @micropost.save
                flash[:notice] = "We shared your message with the selected users"
              else
                flash[:error] = "Sorry! We could not share your message !"
              end
            end

            return    
      else
          flash[:error] = "Sorry! We could not share your message !"
          redirect_to request.referer
          return
      end
   }

   format.html {    
      @user = User.find(params[:user_id].to_i)
      @micropost = @user.microposts.build(params[:micropost])

      if @micropost.save
        flash[:notice] = "We shared your message successfully!"
        redirect_to request.referer
      else
        flash[:error] = "Sorry! We could not share your message !"
        redirect_to request.referer
      end
    }
 end # end of respond to
end
于 2013-09-24T08:36:19.920 回答