Mdjon26
Mdjon26

Reputation: 2255

Re-directing path based on params passed in views

I have the following two buttons in my index.html.haml:

= button_to t('payer_contracts.new_global_payer_contract'), new_payer_contract_path, {:class => 'btn pull-left', :method => 'get'}
= link_to t('payer_contracts.new_bpci_payer_contract'), new_payer_contract_path

As you can see they both go to new_payer_contract_path. How can I make it so that if the first button is clicked, I can send a param such as new_payer_contract_path(new_global_payer_contract) ?

The problem is: in my new.html.haml, both of them are rendering 'global_form'. I want the first button to render global_form and the second button to render 'bpci_form'

Here is the new.html.haml:

- content_for(:title, t('payer_contracts.page_titles.new'))

- content_for :article do
  .common-main-container
    %h1= t('payer_contracts.new_payer_contract')
    = render 'global_form'

In my payer_contracts_controller I have this so far:

def new

    @payer_contract = PayerContract.new(
         id: '',
         contract_name: '',
         contract_type: {
             id: '',
             code_key: '',
             display: '',
             code_group: ''
    },
         amount: '',
         stoploss_amount: '',
         stoploss_reimbursement_percentage: '',
         begin_date: '',
         end_date: '',
         timely_filing_days: '',
         payer: ''
    )
 end 

Upvotes: 3

Views: 23

Answers (1)

Richard Peck
Richard Peck

Reputation: 76784

With button_to, you can pass a params hash:

<%= button_to t('payer_contracts.new_global_payer_contract'), new_payer_contract_path, class: 'btn pull-left', method: :get, params: { x: "y" } %>

This creates a hidden_field inside the form that button_to creates, allowing you to pass params[:x] to your next action.


Update

As per your comments, you're getting the params[:x] variable in your controller.

You just have to use this in your ActiveRecord query call:

#view
-if params["x"] == "y"
  = render "global_form"
-else
  = render "bpci_form"

Upvotes: 1

Related Questions