【问题标题】:How to allow users to share their <modelname> with each other?如何允许用户彼此共享他们的 <modelname>?
【发布时间】:2015-12-18 16:20:28
【问题描述】:

我有用户和文件夹模型,每个用户都有自己的文件夹(has_many/belongs_to)。我需要做的是允许用户彼此共享他们的一些文件夹,因此共享文件夹也成为他们的文件夹。我知道,我可以复制它们,但我正在寻找不那么暴力的方法。

我找到了this,它很棒,但是太旧了——这些方法在 Rails 4 中不起作用。为 Rails 4 重写这段代码的正确方法是什么?

app/views/layout/application.html.erb

<head> 
  <title>ShareBox |<%= content_for?(:title) ? yield(:title) : "Untitled" %></title> 
  <%= stylesheet_link_tag "application", "redmond/jquery-ui-1.8.9.custom" %> 
<%= javascript_include_tag "jquery-1.4.4.min", "jquery-ui-1.8.9.custom.min" %> 
<%= javascript_include_tag "application" %> 

<!-- This is for preventing CSRF attacks. -->
  <%= javascript_include_tag "jquery.Rails" %> 


  <%= csrf_meta_tag %> 
  <%= yield(:head) %> 
</head>

app/views/home/index.html.erb

<div id="invitation_form" title="Invite others to share" style="display:none"> 
    <% form_tag '/home/share' do -%> 
            <label for="email_addresses">Enter recipient email addresses here</label><br /> 
            <%= text_field_tag 'email_addresses', "", :class => 'text ui-widget-content ui-corner-all'%> 
            <br /><br /> 
            <label for="message">Optional message</label><br /> 
            <%= text_area_tag 'message',"",  :class => 'text ui-widget-content ui-corner-all'%> 
            <%= hidden_field_tag "folder_id" %> 
    <% end -%>                 
</div>

分享链接:

<%= link_to "Share", "#", :folder_id => folder.id, :folder_name => folder.name %>

资产/javascripts.js

$(function () {  
    //open the invitation form when a share button is clicked 
    $( ".share a" ) 
            .button() 
            .click(function() { 
                //assign this specific Share link element into a variable called "a" 
                var a = this; 

                //First, set the title of the Dialog box to display the folder name 
                $("#invitation_form").attr("title", "Share '" + $(a).attr("folder_name") + "' with others" ); 

                //a hack to display the different folder names correctly 
                $("#ui-dialog-title-invitation_form").text("Share '" + $(a).attr("folder_name") + "' with others");  

                //then put the folder_id of the Share link into the hidden field "folder_id" of the invite form 
                $("#folder_id").val($(a).attr("folder_id")); 

                //Add the dialog box loading here 
                $( "#invitation_form" ).dialog({ 
    height: 300, 
    width: 600, 
    modal: true, 
    buttons: { 
        //First button 
        "Share": function() { 
            //get the url to post the form data to 
            var post_url = $("#invitation_form form").attr("action"); 

            //serialize the form data and post it the url with ajax 
            $.post(post_url,$("#invitation_form form").serialize(), null, "script"); 

            return false; 
        }, 
        //Second button 
        Cancel: function() { 
            $( this ).dialog( "close" ); 
        } 
    }, 
    close: function() { 

    } 
});  
                return false; 
            }); 
});

使用上面的代码,单击共享链接不会执行任何操作。对话框不出现。我应该改变什么?

【问题讨论】:

    标签: javascript jquery ruby-on-rails ruby-on-rails-4 associations


    【解决方案1】:

    由于多个用户可以拥有同一个文件夹,因此启用此功能的第一步应该是通过中间模型在用户和文件夹之间建立多对多关系。比如:

    class User
      has_many :shared_folders
      has_many :folders, through: :shared_folders
    end
    
    class Folder
      has_many :shared_folders
      has_many :users, through: :shared_folders
    end
    
    class SharedFolder
      belongs_to :user
      belongs_to :folder
    end
    

    如何实现前端取决于您,但创建新SharedFolder 的表单需要将user_idfolder_id 传递给控制器​​。

    【讨论】:

    • 当然,这正是我所做的——在我链接的教程中进行了解释。问题只出在js表单上。
    猜你喜欢
    • 2019-07-27
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多