Ruby 2.0.0,Rails 4.0.3
我有一个_new部分。但是,我用实际存在的实例渲染它。这是必要的,这样我就可以在View和Controller之间通过JavaScript传递实例ID,因为我实际上无法传递实例本身。我只是在新方法中调用Class.first,以便在整个过程中使用现有实例。
我的问题是_new部分提交按钮识别出该实例已存在。这导致它更新而不是创建。按钮字面上说更新。按下时,它将路由到更新方法。那不是我想要的。我想要create方法,我将在其中创建一个填充了所收集参数的新实例。
怎么办?我是否错误地携带一个虚拟实例来启动该过程?如果是这样,什么是正确的解决方案?如果这是可以接受的,我如何强制创建按钮而不是更新?
所有的帮助和评论都表示赞赏。
编辑:我已尝试按钮上的变体以尝试强制它触发新方法。它继续着火更新。我上次失败的努力是:
<button type="submit" formaction="new_admin_car_path" class="btn btn-default btn btn-primary">Create Car</button>
...结束编辑...
表格是:
<div class="span8">
<% car_id = @car.id %>
<%= simple_form_for [:admin, @car],
defaults: {label: false},
html: {id: 'new_admin_car', class: 'form-vertical', method: post},
wrapper: :vertical_form,
wrapper_mappings: {
check_boxes: :vertical_radio_and_checkboxes,
radio_buttons: :vertical_radio_and_checkboxes,
file: :vertical_file_input,
boolean: :vertical_boolean
} do |f| %>
<%= f.input(:stock_number, {input_html: {form: 'new_admin_car', car: @car, value: nil}, autocomplete: :off, placeholder: 'Stock number?'}) %>
<%= f.input(:ymm_year_id, {input_html: {form: 'new_admin_car', car_id: car_id, value: nil}, collection: YmmYear.all.order("year desc").collect{|c| [c.year, c.id]}, prompt: "Year?"}) %>
<%= render partial: "makes", locals: {form: 'new_admin_car', car_id: car_id} %>
<%= render partial: "models", locals: {form: 'new_admin_car', car_id: car_id} %>
<%= f.association(:color, {input_html: {form: 'new_admin_car', value: nil}, autocomplete: :off, prompt: 'Color?'}) %>
<div class="col-xs-6 col-sm-3">
<br/>
<input type="submit" form="new_admin_car" value="Create Car" class="btn btn-default btn btn-primary">
<% end %>
</div>
</div>
部分:
<% unless car_id.blank? %>
<% car = Car.find(car_id) %>
<%# car.ymm_make_id = nil %>
<%= simple_form_for [:admin, car],
defaults: {label: false},
remote: true do |f| %>
<% makes ||= "" %>
<% make = "" %>
<% make = car.make_id if car.class == Car and Car.exists?(car.id) %>
<% if !makes.blank? %>
<%= f.input :ymm_make_id, {input_html: {form: form, car: car, car_id: car.id, value: make}, collection: makes.collect { |s| [s.make, s.id] }, prompt: "Make?"} %>
<% else %>
<%= f.input :ymm_make_id, {input_html: {form: form, car: car, car_id: car.id, value: make}, collection: [], prompt: "Make?"} %>
<% end %>
<% end %>
<% end %>
控制器车辆方法新的地方只有nils所有字段:
def new
@car = Car.first
@car.clear
end
呈现形式:
表单的JavaScript是:
// when the #year field changes
$("#car_ymm_year_id").change(function () {
// make a GET call and replace the content
// First select identifies what has been selected, or fired
var year = $('select#car_ymm_year_id :selected').val();
// Pull the variables from the input_html tag
var form = $('select#car_ymm_year_id').attr("form");
var car_id = $('select#car_ymm_year_id').attr("car_id");
// Routes to the controller action
$.post('/admin/cars/make_list/',
{
form: form,
year: year,
car_id: car_id
},
function (data) {
$("#car_ymm_make_id").html(data);
});
return false;
});
控制器方法:
def make_list
makes = YmmMake.makes(params[:year])
#@car = Car.find(params[:car_id])
render partial: "makes", locals: {car_id: params[:car_id], form: params[:form], makes: makes}
end
答案 0 :(得分:1)
如果我理解正确,您希望实现以下目标:
显示用于创建新车型的表单。在这种形式下,用户输入一年;系统然后从服务器加载该年份的所有品牌,并提供一个选择下拉列表供用户选择品牌。
Car model belongs_to ymm_make,因此我们在提交表单时包含所选的ymm_make_id。
以下是我将如何解决这个问题。我将使用标准的Rails表单助手,这样我们就可以减少一个抽象层了。
表单(带Car.new
):
<%= form_for [:admin, Car.new] do |f| %>
<%= f.text_field :stock_number, autocomplete: "off", placeholder: "Stock number?" %>
<%= text_field_tag :year_search, nil, placeholder: "Year" %>
<%= f.select :ymm_make_id %>
<!-- skipping models and colors here for the sake of brevity -->
<%= f.submit "Create" %>
<% end %>
对于年份搜索字段,我使用text_field_tag
而不是f.text_field
,因为我不希望在提交整个表单时将搜索字段值作为汽车的一部分提交。我暂时将下拉字段留空 - 我们将通过Javascript和JSON填充它。
对于make列表,我将创建一个返回JSON的资源控制器:
class YmmMakesController < ApplicationController
respond_to :json
def index
@makes = YmmMake.makes(params[:year])
respond_with @makes
end
end
不要忘记此控制器的route.rb条目,例如
namespace :admin do
resources :ymm_makes, only: :index
end
我们将在Javascript中使用JSON中的<select>
个选项:
$("input[name=year_search]").change(function () {
// send a GET request to /admin/ymm_makes with the 'year' parameter
// set to the value of the year_search text field
$.getJSON( "/admin/ymm_makes", {year: $(this).val()}, function(data) {
var options_html = [];
// iterate over the JSON that we received back; each entry is one 'ymm_make'
// in JSON form
$.each( data, function( index, make ) {
// make a new <option> tag for each make and push it into the options_html array
// I assume here that YmmMake has an attribute called 'name' you want to display
options_html.push( "<option value='" + make.id + "'>" + make.name + "</option>" );
});
// put all our generated <options> tags into the <select> tag
$('select#car_ymm_make_id').html( options_html.join('') );
});
});
完成所有这些后,您应该有一个工作表单来创建与YmmMake模型对象关联的新车。
关于您现有代码的一些观察结果:
嵌套表单
<%= simple_form_for [:admin, @car] do |f| %>
<%= render partial: "makes", locals: {form: 'new_admin_car', car_id: car_id} %>
<% end %>
如果我看到这一点,那么'make'部分也包含一个表单,因此您创建的<form>
嵌套在<form>
中。 HTML does not allow that。
错误的结束标记顺序
<div class="col-xs-6 col-sm-3">
<br/>
<input type="submit" form="new_admin_car" value="Create Car" class="btn btn-default btn btn-primary">
<% end %>
</div>
结束</div>
必须在<% end %>
之前。如果您构建无效的HTML,则可能会出现奇怪的视觉行为和Javascript错误。
冗余参数
<%= simple_form_for [:admin, @car],
html: {id: 'new_admin_car', class: 'form-vertical', method: post},
do |f| %>
“id”和“method”应该是默认值。离开它们可以使代码更容易阅读。
通过input_html无效的<input>
属性
<%= f.input(:stock_number, {input_html: {form: 'new_admin_car', car: @car, value: nil}, autocomplete: :off, placeholder: 'Stock number?'}) %>
我不熟悉simple_form,但从我看到的情况来看,input_html
用于向input元素添加属性。如上所述的行将因此产生具有无效car
属性的文本输入。一旦删除嵌套表单,就不再需要form
属性。
错误的HTTP方法
$.post('/admin/cars/make_list/'
您通过POST AJAX请求加载您的品牌。对于仅返回数据但不更改任何内容的请求,GET通常更合适。
答案 1 :(得分:0)
在表单中,您可以指定提交按钮应转到的路径:
<%= simple_form_for [:admin, @car], url: create_action_path
...
答案 2 :(得分:0)
我认为您的答案相当简单,只需create
代替update
,只需清除模型的id
即可。
示例:@car.id = nil