How do I put params into this line? - ruby-on-rails

I want to make a button, that changes Object's boolean value.
Have this line:
= button_to 'Subscribe Back', candidate_subscriber_path(subscriber), method: :patch
Which makes this request:
{"_method"=>"patch", "action"=>"update", "controller"=>"candidate/subscribers", "id"=>"2"}
I need it to send these attributes:
subscribed: true
Have no idea where to put them. Do you have any clues?
Tried:
= button_to 'Subscribe Back', candidate_subscriber_path(subscriber, subscribed: true), method: :patch
= button_to 'Subscribe Back', { controller: 'subscribers', action: 'update', id: subscriber.id, subscribed: false }, method: :patch
Couple other options.
The only option that works for me is:
= simple_form_for :subscriber, url: candidate_subscriber_path(subscriber),
html: { method: :patch } do |f|
= f.input :subscribed, as: :hidden, input_html: { value: '1' }
= f.button :submit, 'Subscribe Back'

the way that I would approach it would be to create an action specifically for that.
add to your candidate route -
patch "/resubscribe/:id", to: "candidate/subscribers#resub"
then in the controller
def resub
user = YourModel.find(params[:id])
user.update_attributes( subscribed: true )
end
then just send that path to your button

Related

SimpleForm in HAML

I currently have a simpleForm in a HAML/RoR program:
= simple_form_for(#client, url: {action: "update" }, html: { method: :put }) do |f|
BLAH BLAH BLAH
= f.submit "Update Password", id: "submit", class: "btn btn-primary"
It functions. But on submit, it doesn't go where I want it to go.
When I change it to:
= simple_form_for(#client, url: admin_client_path(#client), html: { method: :put }) do |f|
It goes to the right place if I reload the form page, but the original place otherwise... It appears to actually change the password in both cases, at least...

How to avoid throwing into the URL stuff I don't want for a form with GET

I have a form tag:
= form_tag entities_path, method: :get do
= select(....)
= select(....)
= button_to "Search", name: nil
When I click a button, it puts into the url many things I don't want such as authenticity_token and utf-8. How can I avoid that? I only want 2 selects and their values in the url.
These attributes can be disabled:
= form_tag entities_path, method: :get, authenticity_token: false, enforce_utf8: false do
= select(....)
= select(....)
= button_to "Search", name: nil

How to add 2 different flags for button clicks so that can add extra stuff to html view in Rails?

I am having 2 button fields which are redirected to same html page,
= link_to url_for(params.merge(action: 'my_report', format: :html)), class: 'button on-dark right', data: { toggle: 'modal', placement: 'left' } do
= t('reports.pdf_export')
= link_to url_for(params.merge(action: 'show', format: :pdf)), class: 'button on-dark right' do
= t('reports.all_pdf_export')
In that html I want to add extra stuff when we click on all_pdf_export button, can we add this by name or any flag to be added?
Please help me.
controller method:
def customer_report
#x = params[:x]
#y = params[:y]
#details = Detail.by_account(current_account).active.includes(:company, :primary_contact)
respond_to do |format|
format.html{ render layout: "modal" }
end
end
Button view:
= link_to url_for(params.merge(action: 'my_report', format: :html)), class: 'button on-dark right', data: { toggle: 'modal', placement: 'left' } do
= t('reports.pdf_export')
= button_to t('reports.all_pdf_export'), url_for(params.merge(action: 'customer_report', format: :html)), class: 'button on-dark right', params: { x: "value", y: "value" }, data: { toggle: 'modal', placement: 'left' } do
common view for both the links:
<div>
<%= #account.name %> <br/>
<%= #account.description %><br/>
<% if #x? %>
<%= #account.details %>
<% end %>
</div>
You'll probably be best using button_to, which creates a small form, allowing you the luxury of passing params through it:
= button_to t('reports.all_pdf_export'), url_for(params.merge(action: 'show', format: :pdf)), class: 'button on-dark right', params: { x: "value", y: "value" }
(button_to) generates a form containing a single button that submits to the URL created by the set of options. This is the safest method to ensure links that cause changes to your data are not triggered by search bots or accelerators.
If using the above, you'll be able to access the parameters in your controller as follows:
#app/controllers/your_controller.rb
class YourController < ApplicationController
def show
#x = params[:x]
#y = params[:y]
end
end
Update
You must remember to evaluate against the variable you set:
<%= #account.name %> <br/>
<%= #account.description %><br/>
<%= #account.details if #x == "value" %>

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

When setting Default: Disabled on a form, some Boolean options get disabled?

I have the following line that generates a form. When called, the controller sets #disabled to true in some cases, false in others.
= simple_form_for [:applicant, #application ],defaults: { disabled: #disabled }, url: wizard_path, method: :put do |f|
On some pages, I have a select statement that uses boolean options.
= f.input :accommodation, as: :select, collection: yes_no
That calls the following helper function as a collection
def yes_no
[
['Yes', true ],
['No', false ]
]
end
When #disabled is set to false, and simple_form goes to render the false option, it seems to disable it. I'm unsure how to get around this boolean-fighting to generate both the true and false options in the drop down, while also allowing for #disabled to disable the entire form (when applicable)
Known issue between SimpleForm and Rails: https://github.com/plataformatec/simple_form/issues/1257
tonywok's answer seems to be a valid workaround for now: https://github.com/plataformatec/simple_form/issues/1257#issuecomment-119226834
Change
= simple_form_for [:applicant, #application ],defaults: { disabled: #disabled }, url: wizard_path, method: :put do |f|
To
= simple_form_for [:applicant, #application ],defaults: { disabled: #disabled, readonly: nil }, url: wizard_path, method: :put do |f|

Resources