【问题标题】:Call create method from index page - Rails 7从索引页面调用创建方法 - Rails 7
【发布时间】:2022-08-16 15:22:09
【问题描述】:

我有一个文档模型,我正在寻找去创造一个文件通过单击从我的索引页面发送一些参数的按钮. 我想在没有通过的情况下执行此操作\'新的\'页。

我想要做的是:我单击按钮,通过传递参数创建我的模型,然后重定向到编辑页面以自定义此文档


在我的索引视图中,我使用此按钮:<%= button_to \"Edit\", {:controller => \"documents\", :action => \"create\", :name=>\"doc_name\", :user_id=> current_user.id}, :method=>:post%>

在我的 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

有没有人可以指导我这样做?

谢谢你们


更新

我只是将我的 button_to 更改为这个:

      <%= button_to \"Edite\", {:controller => \"documents\", :action => \"create\", :document=>{:name=>\"doc_name\", :user_id=> current_user.id}}, :method=>:post, class:\"btn-primary\" %>
  • 欢迎来到 SO!获得正确帮助和正确答案的最佳机会是尽可能具体地说明您的问题。尝试发布您遇到的具体问题或错误或挂断。

标签: ruby-on-rails ruby-on-rails-7 rails7


【解决方案1】:

如果我对您的理解正确,您希望:

  1. 单击一个按钮
  2. 创建新文档
  3. 被重定向到该新文档的编辑视图

    如果您永远不需要登陆DocumentsController#new,那么您可以将DocumentsController#new 的最终操作变为redirect_to DocumentsController#edit 以获取document

    如果您想保留DocumentsController#new 的正常角色,只需创建一个新的路由和控制器操作,例如DocumentsController#create_from_index

    然后,您可以在您的索引页面上有一个表单(带有提交按钮),该表单会发布到您的新路线。

    使用表格是最简单的方法。如果您不想使用表单,则需要将按钮数据 AJAX 到控制器操作,这有点复杂(but still very doable

    例如:

    # 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
    

【讨论】:

  • 感谢您的评论我决定保留默认路由并修改了 create 方法。此外,我只是像这样修改我的 button_to :&lt;%= button_to "Edite", {:controller =&gt; "documents", :action =&gt; "create", :document=&gt;{:name=&gt;"doc_name", :user_id=&gt; current_user.id}}, :method=&gt;:post, class:"btn-primary" %&gt; 它的工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多