Call create method from index page - Rails 7

400 Views Asked by At

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. 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


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%>

And in my document_controller I have this :

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


UPDATE

I just change my button_to for this one :

      <%= button_to "Edite", {:controller => "documents", :action => "create", :document=>{:name=>"doc_name", :user_id=> current_user.id}}, :method=>:post, class:"btn-primary" %>
1

There are 1 best solutions below

1
Chiperific On

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.

If you want to preserve the normal role of DocumentsController#new, just create a new route and controller action, e.g. 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)

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