0

Okey, so im kinda new to ruby on rails. Im trying to call a function when im pressing a button. But i have no idea how to do this.

This is basicaly what im trying to do:

<% button_to 'Ban User', call_this_function %>

<% this_function %>
 @user = User.find(:id)
 @user.deactivated("true")
<% end %>

Here is some actual code that I wrote

  <% if has_role?(:admin) %>
    <% if [email protected] %>
      <% if [email protected]_role?(:admin) %>
        <%= link_to 'Ban User', new_discussion_path, class:"button is-danger" %>
      <% end %>
    <% else %>
      <%= link_to 'Unban User', new_discussion_path, class:"button is-success" %>
    <% end %>
  <% end %>

But this only redirects to a page to where I can create a new discussion. What I'm trying to do is to just call a named function when impressing the button, but do not redirect to some other page. I'm sorry for the lack of code, but please ask if you need some clarification.

1
  • It looks like this getting started might help you. Basically you'd need to generate model, controller and view. Example updating articles could be be close to your case as you could update User with that @user.deactivated("true") Commented Aug 19, 2019 at 15:51

1 Answer 1

1

add a controller action to your corresponding controller like,

def ban_user
 @user = User.find(params[:id])
 @user.deactivated("true")
end

add this action to your routes.rb

get 'controller/ban_user'

now you can add the link to your views

<%= link_to 'Ban User', controller_user_ban_path(:id => @user.id), class:"button is-danger", remote: true %>

this will call the function without loading the page.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.