我想要做的是通过JQuery ajax将data-id值传递给外部链接.模式将显示但data-id属性未发送到外部链接.我认为我的Jquery脚本出了问题.但我找不到它.
这是我的链接:
<a href="javascript:;" data-id="1" onclick="showAjaxModal();" class="btn btn-primary btn-single btn-sm">Show Me</a>
Run Code Online (Sandbox Code Playgroud)
这是我的Jquery ajax脚本:
<script type="text/javascript">
function showAjaxModal()
{
var uid = $(this).data('id');
jQuery('#modal-7').modal('show', {backdrop: 'static'});
jQuery.ajax({
url: "test-modal.php?id=" + uid,
success: function(response)
{
jQuery('#modal-7 .modal-body').html(response);
}
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
这是我的模态代码:
<div class="modal fade" id="modal-7">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Dynamic Content</h4>
</div>
<div class="modal-body">
Content is loading...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-info">Save changes</button>
</div>
</div> …Run Code Online (Sandbox Code Playgroud) 我有一个form_for在Rails中使用该collection_check_boxes方法的一系列部件列为复选框.我希望复选框的标签是两种方法的组合,以便每个部分的复选框都标有它的名称和它的SKU编号:
我的表格代码:
<%= collection_check_boxes(:service, :part_ids, @parts_list, :id, :sku, {}, {checked: true}) %>
Run Code Online (Sandbox Code Playgroud)
这非常适用于创建复选框列表.但每个标签只是:sku因为我只能传递一种标签方法.有谁知道为这些创建自定义标签的方法?我已经阅读了api文档,它提到了使用do |b| ... end块但没有解释如何使用它或为什么.
我开始使用Ruby on Rails,我遇到了一些问题.我有一个包含3个字段的表单,这是代码:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.text_field :name, autofocus: true, placeholder: "Name" %>
</div>
<div class="field">
<%= f.email_field :email, autofocus: true, placeholder: "Email" %>
</div>
<div class="field">
<%= f.number_field :age, autofocus: true, placeholder: "Age" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在电子邮件字段中,当您编写非电子邮件并尝试提交的内容时,浏览器(chrome或firefox)会显示错误消息,指出该字段必须包含@.年龄字段也是如此,如果输入了一个字母,浏览器会显示一个错误,表示该字段只接受数字.
我想知道当你尝试提交时,如果任何字段为空,浏览器会显示一条消息.我知道如何在cakephp中做到这一点,所以我想它也可以在ruby中完成.我已经验证了模型中的字段,将显示设置为true,但这仅适用于在您提交并再次重新加载页面后显示消息.
我正在尝试使用本教程中的信息来学习有关多态关联的问题。我有以下几点:
关注/taggable.rb:
module Taggable
extend ActiveSupport::Concern
included do
has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings
end
def tag_list
tags.map(&:name).join(', ')
end
def tag_list=(names)
self.tags = names.split(',').map do |name|
Tag.where(name: name.strip).first_or_create!
end
end
module ClassMethods
def tag_counts
Tag.select('tags.*, count(taggings.tag_id) as count').joins(:taggings).group('taggings.tag_id')
end
end
end
Run Code Online (Sandbox Code Playgroud)
模型/article.rb:
class Article < ApplicationRecord
include Taggable
def self.tagged_with(name)
Tag.find_by!(name: name).articles
end
end
Run Code Online (Sandbox Code Playgroud)
models / tagging.rb:
class Tagging < ApplicationRecord
belongs_to :tag
belongs_to :taggable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
型号/tag.rb:
class Tag < …Run Code Online (Sandbox Code Playgroud) 我的 Rails 已经生锈了,我快把自己逼疯了,试图找出一些可能是如此基本的东西,以至于我看不到树木的森林。
我有一个表单,用于create我的控制器的操作。当我保存时,我还需要更新另一个表中的一些项目。我想把它全部包装在一个交易中。我认为首选是将交易放入模型中。当我这样做并尝试调用该create操作时,它会出错,告诉我该类没有方法。
Equipment_swaps_controller.rb
def create
respond_to do |format|
@equip_swap = EquipSwap.new(equip_swap_params)
if @equip_swap.trans_equip_and_save
format.html { redirect_to @equip_swap, notice: 'Equipment transfer was successfully created.' }
else
format.html { render action: 'failure' }
end
end
end
Run Code Online (Sandbox Code Playgroud)
模型equip_swap.rb
def self.trans_equip_and_save
EquipSwap.transaction do
Tool.transfer_equipment(self.to_vehicle, self.items_moved)
self.save
end
end
Run Code Online (Sandbox Code Playgroud)
具有所需方法的工具模型
def transfer_equipment(location,ids)
ids.each do |id|
Tool.find(id).update(location: location)
end
end
Run Code Online (Sandbox Code Playgroud)
我想调用类的方法将允许它来执行我的的实例方法EquipSwap的实例@equip_swap。当我尝试提交表单,并创建它告诉我,有没有新的记录trans_equip_and_save的方法Class....。有一些明显的东西我错过了。帮助!