SimpleForm in HAML - ruby-on-rails

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

Related

Rails 7.0.4 and data confirm for link_to doesn't work

My last chance is some last living developer in RoR
Nothing in the darkest corners of the internet works for:
<%= link_to 'some-link', '#', data: { confirm:'Are You Sure?') %>;
it generate this:
<a data-confirm="Are you sure?" href="#">some link</a>
but there is no confirm dialog box.
I try everything - turbo_confirm, try to add controller with class for stimulus ... and still nothing.
Right now I remove turbo-rails, stimulus etc. - but still nothing.
data-turbo-confirm presents a confirm dialog with the given value. Can
be used on form elements or links with data-turbo-method.
https://turbo.hotwired.dev/reference/attributes
Examples that work
<%= link_to "GET turbo link", "/",
data: {
turbo_method: :get,
turbo_confirm: "Sure?"
}
%>
<%= link_to "POST turbo link", "/",
data: {
turbo_method: :post,
turbo_confirm: "Sure?"
}
%>
<%= button_to "Button", "/",
data: { turbo_confirm: "Sure?" }
%>
<%= form_with url: "/",
data: { turbo_confirm: "Sure?" } do |f| %>
<%= f.submit "Submit" %>
<% end %>
Doesn't work
<%= link_to "GET link", "/",
data: { turbo_confirm: "Sure?" }
%>
Fix by adding turbo_method: :get.
Really doesn't work
<%= link_to "UJS link", "/",
data: { confirm: "Sure?" }
%>
But easy to fix:
// app/javascript/application.js
document.addEventListener("click", event => {
const element = event.target.closest("[data-confirm]")
if (element && !confirm(element.dataset.confirm)) {
event.preventDefault()
}
})
data-disable-with
This one is a bit more involved, but it's all the same pattern, listen for event, check for data attribute, if else, do something.
// app/javascript/application.js
// on form submit
// set `value` if `input`
// set `innerHTML` if `button`
document.addEventListener("turbo:submit-start", event => {
const element = event.detail.formSubmission.submitter
if (element.hasAttribute("data-disable-with")) {
// element.disabled = true // this is done by turbo by default
if (element instanceof HTMLInputElement) {
element.value = element.dataset.disableWith
} else {
element.innerHTML = element.dataset.disableWith
}
}
})
<%= form_with url: "/" do |f| %>
# <button>
<%= f.button "Submit", data: { disable_with: "..." } %>
# <input>
<%= f.submit "Submit", data: { disable_with: "..." } %>
<% end %>

how to process form_with using GET request as XHR

I'm working on a Rails 6 app, and want to update page view based on a dropdown value selected using XHR. Dropdown must use GET method coz I am calling index action.
I am using form_with which by default uses remote: true.
I am not using local: true.
I tried onchange: "Rails.fire(this.form, 'submit')" - this does send XHR request and receives a response but does not update view.
I tried onchange: "this.form.submit();" - this does a full page reload not utilizing XHR.
Code from app/views/users/index.html.erb
<%= form_with url: station_users_path(station_id: Current.user.home_station), method: :get do |form| %>
<%= form.select :user_status, options_for_select( { "Active users" => "unlocked", "Inactive users" => "locked"}, #user_status ), {}, { :onchange => "Rails.fire(this.form, 'submit')" } %>
<% end %>
Code from app/controllers/users_controller.rb
def index
#user_status = params[:user_status] || "unlocked"
#users = #station.users.send(#user_status) || []
#user_status == "unlocked" ? seperate_managers_from_users : #managers = []
end
In onchange just write one get_station_users() function. Inside that you can use ajax calling.
In forms
<%= form_with url: station_users_path(station_id: Current.user.home_station), id: “form_id”, method: :get do |form| %>
<%= form.select :user_status, options_for_select( { "Active users" => "unlocked", "Inactive users" => "locked"}, #user_status ), {}, { :onchange => "get_station_users()" } %>
<% end %>
Add Script
function get_station_users(){
$.ajax({
type: "GET",
url: "/station_users",
data: { $('#form_id').serialize(), },
dataType: 'script'
});
}
Your response will be as JS. So you can use index.js.erb

Achieve rendering of Semantic UI Icon within button_to

I am trying to achieve a button_to which looks like a link (class="btn btn-link") and which has as its text (input) value a semantic-ui icon. I need the button to display in-line as a link. I am using the semantic-ui-rails gem. My button-to is formatted as below:
<%= button_to semantic_icon('lightbulb outline'),
votes_path,
options: {
params: {
voter_id: current_user.id,
target_type: "Comment",
target_id: comment.id,
value: 1
}
},
class: "btn btn-link",
remote: true
%>
However, the button displays on my screen as
<i class="lightbulb outline icon"></i>
Instead of rendering the lightbulb-outline icon itself.
What must I change to get the value of the input tag of the button-to form to render properly?
try
<%= button_to votes_path, class: "btn btn-link", remote: true, params: {
voter_id: current_user.id,
target_type: "Comment",
target_id: comment.id,
value: 1
} do %>
<%= semantic_icon('lightbulb outline') %>
<% end %>

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

How do I put params into this line?

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

Resources