如何允许用户共享他们的<型号名称>彼此之间
How to allow users to share their <modelname> with each other?
我有用户和文件夹模型,每个用户都有自己的文件夹(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_id
和folder_id
传递给控制器。
相关文章:
- 在<页眉>标签
- 函数参数中的数据与指定变量之间的任何性能差异
- 全局变量和全局对象的属性之间有什么区别吗
- java.net和javascript之间正则表达式的差异
- 如何更改<svg>标记为<img>用js标记
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- JavaScript中的函数和对象之间没有区别吗?
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- Jquery在函数之间传递表行
- 如何附加<ul><李>元素位于某个特定条件的父元素列表之间
- MeteorJS-函数()和()之间的差异=>
- 如果两个<img>彼此接触
- 如何允许用户共享他们的<型号名称>彼此之间
- Javascript:替换<br>出现在多组括号之间
- 如何附加列表项<李>从无序列表<ul>其他四个未排序列表之间
- 日期列表介于2个日期之间(从->到)
- 确定时间范围的回调函数彼此之间不冲突
- 没有重复的随机数,并且彼此之间有范围
- 添加<br>字符串中的文本之间
- MVC Razor$.Ajax彼此之间工作不好