我们如何修复 nested_attribute: _result_fields.html.erb 以便当用户单击“删除”时它实际上将其删除?而现在点击它什么都不做。
<%= f.text_field :result_value, class: 'form-control', placeholder: 'Enter Result' %>
<%= f.date_select :date_value, :order => [:month, :day, :year], :with_css_classes => true, :class => "date-select" %>
<%= f.check_box :bad %>
<%= link_to_remove_association f do %>
Delete
<% end %>
stats has_many 结果
stats/_form
<div id="results">
<%= f.fields_for :results do |result| %>
<%= render 'result_fields', :f => result %>
<% end %>
</div>
<span class="label label-primary">
<%= link_to_add_association f, :results do %>
<span class="glyphicon glyphicon-plus"></span> Result
<% end %>
</span>
在 stats_controller 中,我将其作为参数:results_attributes: [:id, :result_value, :date_value, :bad, :_destroy]
模型:
class Stat < ActiveRecord::Base
has_many :results
accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true
end
class Result < ActiveRecord::Base
belongs_to :stat
end
我正在使用 cocoon gem .
如果您需要进一步的代码或解释,请告诉我。谢谢!
class StatsController < ApplicationController
before_action :set_stat, only: [:show, :edit, :update, :destroy, :like]
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
def index
if params[:tag]
@stats = Stat.tagged_with(params[:tag])
else
@stats = Stat.joins(:results).all
@averaged_stats = current_user.stats.averaged
@instance_stats = current_user.stats.instance
end
end
def show
@stat = Stat.find(params[:id])
@commentable = @stat
@comments = @commentable.comments
@comment = Comment.new
@notable = @stat
@notes = @notable.notes
@note = Note.new
@correct_user = current_user.stats.find_by(id: params[:id])
end
def new
@stat = current_user.stats.build
end
def edit
end
def create
@stat = current_user.stats.build(stat_params)
if (params[:commit] == 'conceal')
@stat.conceal = true
@stat.save
redirect_to @stat, notice: 'Stat was successfully created'
elsif
@stat.save
track_activity @stat
redirect_to @stat, notice: 'Stat was successfully created'
else
flash.now[:danger] = 'Required Fields: "Averaged or Instance", "Enter Action", "Enter Metric", and "+ Result"'
render 'new'
end
end
def update
if @stat.update(stat_params)
redirect_to stats_url, notice: 'Goal was successfully updated'
else
render action: 'edit'
end
end
def destroy
@stat.destroy
@result.destroy
redirect_to stats_url
end
def like
@stat = Stat.find(params[:id])
@stat_like = current_user.stat_likes.build(stat: @stat)
if @stat_like.save
@stat.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Two many likes'
end
redirect_to(:back)
end
private
def set_stat
@stat = Stat.find(params[:id])
end
def correct_user
@stat = current_user.stats.find_by(id: params[:id])
redirect_to root_url, notice: "Not authorized to edit this stat" if @stat.nil?
end
def stat_params
params.require(:stat).permit(:categories, :like, :action, :metric, :date, :comment, :private_submit, :tag_list, results_attributes: [:id, :result_value, :date_value, :bad, :_destroy])
end
end
stat.js
$( document ).ready(function() {
$('.date-format-switcher').click(function(event){
event;
if ($(this).attr('id') == 'stat_categories_instance') {
$('.day').show();
} else if ($(this).attr('id') == 'stat_categories_averaged') {
$('.day').hide();
}
})
$('.add-form-padding').on('cocoon:after-insert', function(e, insertedItem) {
if($('#stat_categories_instance').is(':checked')) {
$('.day').show();
} else {
$('.day').hide();
}
})
});
最佳答案
如果 link_to_add_association 有效,则 javascript 已正确加载。
仍然对什么不起作用感到困惑。如果单击 link_to_remove_association 没有从页面中明显地删除任何内容,则您缺少正确的包装器类。默认情况下它应该是 .nested-fields,但是可以通过明确指定它来否决它(如 documented )。
但是如果项目在视觉上被删除,并且您认为应该立即将其发送到服务器,那么您就误解了 cocoon 的工作原理:您编辑了一个表单,该表单仅在您提交表单时保存(发送到服务器) .所以 link_to_remove_association 仅可见删除嵌套子项,并编辑表单的内容(设置 _destroy 标志),因此在保存/提交表单时,嵌套子项将被删除。
关于jquery - link_to_remove_association 没有删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30968745/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article