简体   繁体   English

从索引页面调用创建方法 - Rails 7

[英]Call create method from index page - Rails 7

I have a Document model and I'm looking to create a Document by clicking a button that send some params from my index page .我有一个文档 model ,我希望通过单击从我的索引页面发送一些参数的按钮来创建一个文档。 I want to do this whithout passed in the 'new' page.我想在不通过“新”页面的情况下执行此操作。

What I want to do exactly is: I click the button, that create my model with params passed, then redirect to the edit page to custom this document我真正想做的是:我单击按钮,创建我的 model 并传递参数,然后重定向到编辑页面以自定义此文档


In my index view I use this button: <%= button_to "Edit", {:controller => "documents", :action => "create", :name=>"doc_name", :user_id=> current_user.id}, :method=>:post%>在我的索引视图中,我使用这个按钮: <%= button_to "Edit", {:controller => "documents", :action => "create", :name=>"doc_name", :user_id=> current_user.id}, :method=>:post%>

And in my document_controller I have this:在我的 document_controller 我有这个:

def create
@document = Document.new(document_params{params[:user_id]})

respond_to do |format|
  if @document.save
    flash.now[:notice] = "Document créé avec succès."

    format.turbo_stream do
      render turbo_stream: [turbo_stream.append("documents", partial:"documents/document", locals: {document: @document}),
        turbo_stream.update("content-d", partial:"documents/table"),
        turbo_stream.replace("notice", partial: "layouts/flash")]
    end
    format.html { redirect_to document_path(@document), notice: "Document was successfully created." }
    format.json { render :show, status: :created, location: @document }
    
  else
    format.html { render :new, status: :unprocessable_entity }
    format.json { render json: @document.errors, status: :unprocessable_entity }
  end
end
end

def document_params
  params.fetch(:document, {}).permit(:doc_type, :number, :name, :total_ttc, :user_id)
end

Is there someone who can guide me to do this?有没有人可以指导我这样做?

Thank you all谢谢你们

If I understand you correctly, you want to:如果我对您的理解正确,您希望:

  1. Click a button单击一个按钮
  2. Have a new Document be created创建一个新文档
  3. Be redirected to the Edit view of that new Document被重定向到该新文档的编辑视图

If you never need to land on DocumentsController#new , then you can just have the final action of DocumentsController#new be to redirect_to DocumentsController#edit for that document .如果您从不需要登陆DocumentsController#new ,那么您可以将DocumentsController#new的最终操作redirect_to到该documentDocumentsController#edit

If you want to preserve the normal role of DocumentsController#new , just create a new route and controller action, eg DocumentsController#create_from_index如果要保留DocumentsController#new的正常角色,只需创建一个新路由和 controller 动作,例如DocumentsController#create_from_index

Then you can have a form (with a submit button) on your Index page that POSTs to your new route.然后,您可以在您的索引页面上有一个表单(带有提交按钮),该表单发布到您的新路线。

Using a form is your easiest method.使用表格是最简单的方法。 If you don't want to use a form, you'll need to AJAX the button data to a controller action, which is a bit more complicated ( but still very doable )如果您不想使用表单,则需要将 AJAX 按钮数据转换为 controller 操作,这有点复杂(但仍然非常可行

For example:例如:

# routes
Rails.application.routes.draw do
  resources :documents do
    collection do
      post 'create_from_index'
    end
  end
end
# documents_controller
class DocumentsController < ApplicationController
  ...
  def index
    # do your index stuff here

    @new_document = Document.new
  end

  def create_from_index
   # get the params from the form submission (or AJAX request)
   if Document.create(document_params)
     # successful creation
     redirect_to action: :edit
   else
    # do something about errors
    render :index
   end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM