如何允许用户共享他们的<型号名称>彼此之间

How to allow users to share their <modelname> with each other?

本文关键字:彼此之间 gt lt 何允许 用户 共享 他们的      更新时间:2023-09-26

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

我发现了这一点,它很棒,但太老了——这些方法在Rails4中不起作用。为Rails4重写这段代码的正确方法是什么?

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 %>

assets/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; 
            }); 
});

使用上面的代码,单击"共享"链接不会有任何作用。对话框未显示。我应该更改什么?

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

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传递给控制器。