rails 4 update_attribute with unobtrusive javascript - ruby-on-rails

I have the following link which is generated in loop
<% if category.status %>
<%= link_to 'Disable', edit_category_path(category), method: :get, remote: true, confirm: 'Are you sure you want to disable' %>
<% else %>
<%= link_to 'Enable', edit_category_path(category), method: :get, remote: true, confirm: 'Are you sure you want to enable' %>
<% end %>
In my edit action I have
unless params[:id].blank?
#category = #category.update_attribute(:status, false) # Here I mentioned false as static.
end
If I click enable it should enable(update as true in database) and if I click disable it should disable(update as false in database)
How can I do it. How do I pass the status(true or false for updation)

Use it like this
<%= link_to 'Disable', edit_category_path(category, {status: 0}), method: :get, remote: true, confirm: 'Are you sure you want to disable' %>
And you can call as
params[:status] in controller

If 'GET edit_category_path' is the usual 'produce a form which POSTs to #update,' the GET shouldn't update server state.
Maybe POST (probably directly to #update), to change status, then GET the edit_... path
Or POST to a new action that updates status and redirect to #edit?

Related

Ruby Rails delete confirmation not popping up before destroy

I am trying to delete a record from my database using Rails destroy action and button_to, however, the confirmation does not pop up and the app simply deletes the record without confirmation. I've tried multiple approaches:
<%= button_to 'Delete', post_path, :method => :delete, :confirm => 'Are you sure you want to delete this item?' %>
<%= button_to 'Delete', post_path, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= button_to 'Delete',post_path, :method => :delete, data: { confirm: 'Are you sure you want to delete this item?'}
<%= button_to 'Delete', post_path, {method: :delete}, {confirm: 'Are you sure?'} %>
Neither shows a confirmation module.
I was able to get this to work by following this here.
https://dev.to/software_writer/how-to-show-a-delete-confirmation-dialog-in-rails-using-stimulus-17i
Basically what you want to do is create a Stimulus js file in your app/javascript/controllers. In your case it would be posts_controller.js. Then put the following code in posts_controller.js.
import { Controller } from "#hotwired/stimulus"
export default class extends Controller {
// method for alerting before deleting posts
delete(event) {
let confirmed = confirm("Are you sure?");
if(!confirmed) {
event.preventDefault();
}
}
}
When you create the button to delete your post, have the parent div container have the data-controller="posts" attribute. Then set the action for the button_to to your method in your posts_controller.js. It should look something like this.
<div id="postDeleteLink" data-controller="posts">
<%= button_to "Delete this post", #post, method: :delete, data: { action: "click->posts#delete" } %>
</div>

Rails 'remote:true' ignored when params provided

In a Rails (5.2.4.2) app
I am using button_to + remote: true, and this is working as expected -> ajax call fired, all is OK.
But then I add 'params' to the button_to. Params are added to the form as hidden input (as expected) but when I click on the button, the request made is not remote, and all the page content is updated.
Problematic: button_to + remote + params -> this code seems to ignore the remote:true, although I see data-remote="true" in the form tag
<%= button_to ent_path(ent),
{remote: true,
method: :patch,
class:"btn btn-primary",
params: {ent: {active_a: false}}
} do
%>
<span>TEEEST</span>
<% end %>
Working as expected:
<%= button_to ent_path(ent),
{remote: true,
method: :patch,
class:"btn btn-primary",
} do
%>
<span>TEEEST</span>
<% end %>
So my purpose is to have the button_to remote and update the ent record, changing the active_a attribute depending on some logic
You want to pass any extra parameters via the link path, like this
<%= button_to ent_path(ent, active_a: false),
{remote: true,
method: :patch,
class:"btn btn-primary",
} do
%>
<span>TEEEST</span>
<% end %>
Turned out the remote:true was working properly. But there was a redirect_back in the controller that was executed in case of some custom logic, so that appeared as if the button is working without remote:true

ruby-on-rails - custom method and link_to

I have a program with users and projects (many to many relation). I would like to create my own methods: to delete all projects from specific user and to delete specific project from specific user, but I can't handle that. There is (quiet big) possibility I don't understand routes.
Below I insert code to delete all project from specific user.
In my user_controller.rb I have:
def delete_projects_from_user
#user.projects.delete_all
end
In show.html.erb link_to:
<%= link_to 'Delete all projects', #user, method: :delete_projects_from_user, data: { confirm: 'Are you sure?' } %>
And in routes I tried among others this two option:
resources :users do
get 'delete_projects_from_user', on: :member
end
or
post '/users/:id', to: 'users#delete_projects_from_user', as: :delete_projects_from_user
First option trows: "No route matches [POST] "/users/(id)"
Second option just do nothing.
I will be grateful for prompt.
Basic format:
<%= link_to 'DISPLAY TEXT', YOUR_ROUTE_path(#object), method:
:HTTP_METHOD, data: { Additional html params } %>
Here is the solution:
<%= link_to 'Remove All Projects', delete_projects_from_user_path(#user), method: :post, data: { confirm: 'Are you sure?' } %>
Then in your method:
def delete_projects_from_user
user = user.find(params[:id])
user.projects.delete_all
redirect_to :back #if nothing to render
end
I'm sure this may help you.
For this
def delete_projects_from_user
#user.projects.delete_all
end
You better user .destroy_all to make sure this object and all of it's associated items are destroyed as well
.delete_all only delete the object and it leaves the associated entries on the DB
and As for this:
<%= link_to 'Delete all projects', #user, method: :delete_projects_from_user, data: { confirm: 'Are you sure?' } %>
In your route you defined your route as post so it should be
method: :post
to be like this
<%= link_to 'Delete all projects', #user, method: :post, data: { confirm: 'Are you sure?' } %>
And here you haven't added the route correctly, it should be like this
<%= link_to 'Delete all projects', YOUR_ROUTE_path(#user), method: :post, data: { confirm: 'Are you sure?' } %>
While it's recommended to define this route like this
delete '/users/:id', to: 'users#delete_projects_from_user', as: :delete_projects_from_user
As for the 2nd option, you can use collection as well
resources :users do
collection do
delete 'user/:id', to: 'users#delete_projects_from_user', as: :delete_projects_from_user
end
end
and modify the link to be
<%= link_to 'Delete all projects', delete_projects_from_user(#user), method: :delete, data: { confirm: 'Are you sure?' } %>
Both options are fine, and the 2nd one with delete is the recommended one

Route using dot rather than / in rails DELETE method

The delete button I have in my rails app returns the following error :
No route matches [DELETE] "/requests.3"
I'm not sure why.
Here is the button link in the request view (using bootstrap 3) :
<%= link_to '<i class="glyphicon glyphicon-remove"></i>'.html_safe,
requests_path(request), method: :delete,
data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' %>
and in routes I have :
delete 'requests/:id' => 'requests#destroy'
and in the controller I have
def destroy
#request = Request.find(params[:id])
#request.destroy
redirect_to action: 'index', status: 303
end
Any help would be appreciated.
Thank you.
The solution I figured to work was to manually specify the path in the <%= link_to tag
<%= link_to '<i class="glyphicon glyphicon-remove"></i>'.html_safe,
"/requests/#{request.id}/destroy", method: :delete,
data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' %>
and that seemed to work.

How to add form parameter in Rails link with PUT method?

Right now, I have the following and it works:
<%= link_to 'stop service', service_path(:id => #service.id, 'service[stopped]' => true, method: :put, data: { confirm: 'Are you sure?' } %>
The link looks like this: http://localhost:3000/services/1?service%5Bstopped%5D=true
So the service[stopped] parameter gets submitted to the server as part of the query string. I need it to get submitted though as a POST/PUT form parameter instead. I tried adding 'service[stopped]' => true inside the data hash, but that didn't do anything.
You can try this.
<%= link_to 'stop service', service_path(:id => #service.id, 'service[stopped]' => true, data: { confirm: 'Are you sure?' }) , :method => :post %>
You can refer Rails 3 link_to generator for :post, :put, & :delete?

Resources