Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 按钮:创建一个对象,然后重定向以创建另一个?_Ruby On Rails_Ruby_Formtastic - Fatal编程技术网

Ruby on rails 按钮:创建一个对象,然后重定向以创建另一个?

Ruby on rails 按钮:创建一个对象,然后重定向以创建另一个?,ruby-on-rails,ruby,formtastic,Ruby On Rails,Ruby,Formtastic,railsnoobquestion:我正在尝试开发一种功能,用户可以在rails中保存一个对象,然后再次转发到表单,以创建另一个对象 我可以想出两个选择: -创建一条全新的路线 -向restaurant post对象添加数据,是否在控制器中检查该数据 有人构建过类似的功能吗 thx这里是posts资源的标准创建操作 def create @post = Post.new params[:post] if @post.save redirect_to @post, notice:

railsnoobquestion:我正在尝试开发一种功能,用户可以在rails中保存一个对象,然后再次转发到表单,以创建另一个对象

我可以想出两个选择: -创建一条全新的路线 -向restaurant post对象添加数据,是否在控制器中检查该数据

有人构建过类似的功能吗


thx

这里是posts资源的标准创建操作

def create
  @post = Post.new params[:post]

  if @post.save
    redirect_to @post, notice: 'Post was successfully created.'
  else
    render :new
  end
end
您需要做的就是将重定向从@post更改为new_post_路径

将_重定向到新的_post_路径,注意:“post已成功创建。”


这就是您要找的吗?

解决方案是使用另一个提交按钮创建一个隐藏的表单字段:

%input#create_another{:name => "create_another", :type => "hidden", :value => 0 }
%a.btn.btn-info#submit_another
然后使用javascript提交表单:

jQuery(document).ready(function() {
  $("#submit_another").click(function() {
    $('#create_another').attr('value','1');
    console.log($('#create_another').attr('value'));
    $(".formtastic").submit();
    return true;
  });
});
在各自的控制器内,在我的情况下,类别控制器:

if params[:create_another].nil?
  @create_another = false
else
  @create_another = (params[:create_another] == "1")
end
respond_to do |format|
  if @category.save
    if @create_another
      format.html { redirect_to new_restaurant_category_path(@restaurant), notice: I18n.t(:entry_successfully_created, :name => I18n.t(:category_singular)) }

谢谢,这很有帮助。我会发布我的解决方案,类似于你的。