表单提交中的分组参数
Grouping parameters in form submission
我在 rails 中有一个 React 表单组件,它有几个字段。
React 组件文件
var TemplateForm = React.createClass({
render: function () {
return (
<div className="panel">
<div className="panel-header">
Create new Template
</div>
<div className="panel-content">
<form entype="multipart/form-data" ref="template_form" action={this.props.baseUrl} acceptCharset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓" />
<input type="hidden" name='authenticity_token' value={this.props.authenticity_token} />
<ul>
<li className="item">
<label className="f-label field-required">Name</label>
<input className="txt-box medium" name="template-name"/>
</li>
<li className="item">
<label className="f-label field-required">Paper Size</label>
<select id ="paper-size" name="paper-size" className="medium" >
<option>A4</option>
<option>A3</option>
</select>
</li>
<li className="item">
<label className="f-label field-required">Theme</label>
<select id ="theme" name="theme" className="medium">
<option>Daffodils</option>
</select>
</li>
</ul>
</form>
</div>
<div className="panel-footer">
<button className="btn beta-btn" onClick={this.submitForm}>Create Template</button>
</div>
</div>
);
},
submitForm: function(){
this.refs.template_form.submit();
}
});
当我提交它们时,我在服务器控制台中得到以下参数。
{"utf8"=>"✓",
"authenticity_token"=>"some_token",
"template-name"=>"World",
"paper-size"=>"A4",
"theme"=>"Daffodils"}
如何对参数进行分组说我想将 utf8 和 authenticity_token 与其余部分分开。
期待像
这样的输出
{"utf8"=>"✓",
"authenticity_token"=>"some_token",
"template_info" => {
"template-name"=>"World",
"paper-size"=>"A4",
"theme"=>"Daffodils"
}
}
将命名空间添加到您的输入中:
<ul>
<li className="item">
<label className="f-label field-required">Name</label>
<input className="txt-box medium" name="template_info[template-name]"/>
</li>
<li className="item">
<label className="f-label field-required">Paper Size</label>
<select id ="paper-size" name="template_info[paper-size]" className="medium" >
<option>A4</option>
<option>A3</option>
</select>
</li>
<li className="item">
<label className="f-label field-required">Theme</label>
<select id ="theme" name="template_info[theme]" className="medium">
<option>Daffodils</option>
</select>
</li>
</ul>
我在 rails 中有一个 React 表单组件,它有几个字段。
React 组件文件
var TemplateForm = React.createClass({
render: function () {
return (
<div className="panel">
<div className="panel-header">
Create new Template
</div>
<div className="panel-content">
<form entype="multipart/form-data" ref="template_form" action={this.props.baseUrl} acceptCharset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓" />
<input type="hidden" name='authenticity_token' value={this.props.authenticity_token} />
<ul>
<li className="item">
<label className="f-label field-required">Name</label>
<input className="txt-box medium" name="template-name"/>
</li>
<li className="item">
<label className="f-label field-required">Paper Size</label>
<select id ="paper-size" name="paper-size" className="medium" >
<option>A4</option>
<option>A3</option>
</select>
</li>
<li className="item">
<label className="f-label field-required">Theme</label>
<select id ="theme" name="theme" className="medium">
<option>Daffodils</option>
</select>
</li>
</ul>
</form>
</div>
<div className="panel-footer">
<button className="btn beta-btn" onClick={this.submitForm}>Create Template</button>
</div>
</div>
);
},
submitForm: function(){
this.refs.template_form.submit();
}
});
当我提交它们时,我在服务器控制台中得到以下参数。
{"utf8"=>"✓",
"authenticity_token"=>"some_token",
"template-name"=>"World",
"paper-size"=>"A4",
"theme"=>"Daffodils"}
如何对参数进行分组说我想将 utf8 和 authenticity_token 与其余部分分开。
期待像
这样的输出{"utf8"=>"✓",
"authenticity_token"=>"some_token",
"template_info" => {
"template-name"=>"World",
"paper-size"=>"A4",
"theme"=>"Daffodils"
}
}
将命名空间添加到您的输入中:
<ul>
<li className="item">
<label className="f-label field-required">Name</label>
<input className="txt-box medium" name="template_info[template-name]"/>
</li>
<li className="item">
<label className="f-label field-required">Paper Size</label>
<select id ="paper-size" name="template_info[paper-size]" className="medium" >
<option>A4</option>
<option>A3</option>
</select>
</li>
<li className="item">
<label className="f-label field-required">Theme</label>
<select id ="theme" name="template_info[theme]" className="medium">
<option>Daffodils</option>
</select>
</li>
</ul>