我想在点击按钮时显示弹出窗口.弹出窗口应该有文件上传控件.我需要实现上传功能.
基页有嵌套表单.里面嵌套了三种形式.如果我评论这两个表单,那么我可以从Request Object获取发布的文件.但我不想对其他两种形式发表评论.使用嵌套表单,我没有从Request对象获取发布的文件.
我需要一些协议来实现这一点.
我正在使用C#.弹出窗口是使用jQuery设计的.
正如所建议的那样,我在这里发布示例代码.
<form id="frmMaster" name="frmMaster" method="post" action="Main.aspx" Runat="server" enctype="multipart/form-data">
<form method='Post' name='frmSub'>
<input type="hidden" name='hdnData' value=''>
</form> // This form is driven dynamically from XSL
<form method='Post' name='frmMainSub'>
<input type="hidden" name='hdnSet' value=''>
</form>
</form>
Run Code Online (Sandbox Code Playgroud)
评论内部形式很好.但是,因为它不需要触及那些形式的其他功能.
我已将此代码用于示例目的.此页面中的实际LOC为1200.第二个表单动态加载了大量控件.我被要求不要触摸现有的表格.是否可以使用嵌套表单执行此功能?
我正在使用nested_form gem但是遇到了一个给我带来麻烦的用例.我有一个特定的嵌套表单,其中现有嵌套对象的部分不同于添加新对象时所需的部分.(特定用例是图像 - 现有上传图像显示图像标记,新图像对象需要呈现file_field以进行上传.)
我试过的一件事是在f.object.nil的部分内部检查?确定要渲染的内容,但新对象和现有对象仍然生成现有对象的部分代码.我假设这与nested_form BuilderMixin调用fields_for的方式有关?
是否有一种简单的方法可以做到这一点,我错过了?或者我是否需要修改nested_form link_to_add代码才能适应这种情况?
作为参考,这是我尝试使用一个部分:
<tr>
<% if !f.object.nil? %>
<td>
<a href="/customer_images/<%= f.object.id %>" target="_new">
<%= image_tag f.object.picture.url -%>
</a>
</td>
<td>
Description: <%= f.object.image_description %><br/>
Date Uploaded: <%= (f.object.nil? || f.object.created_at.nil?) ? "Not yet uploaded." : f.object.created_at.strftime("%A %h %d, %Y %I:%M %p") %>
<br>
<%= f.link_to_remove "Remove this image" %>
</td>
<% else %>
<td> </td>
<td>
<%= label :image, :file, "Select File:" %><%= f.file_field :picture %><br />
Description: <%= f.text_field :image_description %><br />
</td> …Run Code Online (Sandbox Code Playgroud) 我正在使用nested_form添加和删除nested_attributes.
class Text < ActiveRecord::Base
attr_accessible :attachments_attributes
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments
end
Run Code Online (Sandbox Code Playgroud)
和嵌套模型
class Attachment < ActiveRecord::Base
attr_accessible :description, :file
belongs_to :attachable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
添加附件工作正常,但删除doesent.
单击删除链接后,text[attachments_attributes][0][_destroy]输入值从此更改为false,1所以我认为这不是问题.
我的更新方法:
def update
@text = Text.find(params[:id])
if @text.update_attributes(params[:text])
redirect_to @text, :notice => "Successfully updated text."
else
render :action => 'edit'
end
end
Run Code Online (Sandbox Code Playgroud)
我的更新方法中的params输出是
attachments_attributes:
'0':
description: asdf asdf as fs
_destroy: '1'
id: '2'
'1':
description: ''
_destroy: '1'
id: '3'
'2':
description: …Run Code Online (Sandbox Code Playgroud) 当我搜索时,这是一个常见问题,但我发现没有一个答案适合我的情况。我已经使用设计建立了一个用户模型,它有两个相关的模型,它有一个详细联系信息和许多地址。嵌套表单适用于地址,但未显示我的联系方式详细信息字段。
我的用户模型如下:
validates_presence_of :contact_detail, :addresses
# Include default devise modules. Others available are:
# :lockable, :timeoutable, :trackable and :omniauthable
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
has_one :contact_detail, dependent: :destroy
has_many :addresses, dependent: :destroy
accepts_nested_attributes_for :addresses,
allow_destroy: true
accepts_nested_attributes_for :contact_detail,
allow_destroy: true
Run Code Online (Sandbox Code Playgroud)
联系方式型号只有belongs_to :user
我在我的应用程序控制器上进行了 devise gem 中提到的更改:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [addresses_attributes: [:street_name, :street_number, :city, :country, :postal_code, :name],
contact_detail_attributes: [:first_name, :last_name, :description, :telephone, :mobile ]])
end
end
Run Code Online (Sandbox Code Playgroud)
我的 …