Search code examples
ruby-on-railsform-with

How to use Rails 5.2 form_with to trigger a specific action?


My application needs to duplicate a Skill (from skills index) as many times the user needs it in his cart. So I decided to trigger the add-to-cart method of the skills_controller when the related form, including the number of duplicates and the Skill's id, is submitted. For this purpose, I added counter to the strong parameters of skills_controller.

Unfortunately, I am missing something to correctly setup the form: when submitted, it triggers the create method. Here is the code:

routes.rb extract

  resources :skills, :path => "variables" do
    resources :values_lists
    member do
      post :add_to_cart
      get  :create_values_list
      get  :upload_values_list
      get  :remove_values_list
    end
    collection do
      get :index_all
    end
  end

skills_controller.rb method

  def add_to_cart
    @template_skill = Skill.find(params[:id])
    iterations = params[:skill][:counter].to_i
    until iterations == 0
      @skill = @template_skill.deep_clone include: [:translations, :values_lists]
      @skill.business_object_id = session[:cart_id]
      @skill.template_skill_id = @template_skill.id
      @skill.code = "#{@template_skill.code}-#{Time.now.strftime("%Y%m%d:%H%M%S")}-#{iterations}"
      @skill.is_template = false
      @skill.save
      iterations -= 1
    end

    @business_object = BusinessObject.find(session[:cart_id])
    redirect_to @business_object, notice: t('SkillAdded2BO') # 'Skill successfully added to business object'
  end

index.html.erb table content

  <tbody>
    <% @skills.each do |skill| %>
      <tr data-href="<%= url_for skill %>">
        <% if not session[:cart_id].nil? %>
          <td>
            <%= form_with model: @skill, :action => "add_to_cart", :method => :post, remote: false do |f| %>
            <%= f.text_field :counter, value: "1", class: "mat-input-element", autofocus: true %>
              <button type="submit" class="mat-icon-button mat-button-base mat-primary add-button" title="<%= t('AddToUsed') %>">
                <span class="fa fa-plus"></span>
              </button>
            <% end %>
          </td>
        <% end %>
        <td class="no-wrap"><%= skill.code %></td>
        <td><%= link_to skill.translated_name, skill %></td>
        <td><%= link_to translation_for(skill.parent.name_translations), skill.parent %></td>
        <td><%= skill.responsible.name %></td>
        <td><%= skill.updated_by %></td>
        <td class="text-right"><%= format_date(skill.updated_at) %></td>
      </tr>
    <% end %>
  </tbody>

Thanks a lot for your help!


Solution

  • According to this form helpers guide, the syntax you used doesn't exist

    form_with model: @model, action: :custom_action
    

    So in this case, you have to specify the url parameter for form_with to make it works.

    <%= form_with model: @skill, url: :add_to_cart_skill_path(@skill), method: :post, remote: false do |f| %>