0

I have a model organismereferent which has a boolean value named active. What I'm trying to do is depending on what value it has do a different action.

So if it's active the button shows deactivate and allows the user to set active to false. So after that the button should show activate which will allow the user to set active back to true.

I tried making a method in my controller then calling that method from my index view but it gives me a No route matches [POST] "/organismereferents/8". I'm pretty new to Ruby on Rails so there must be an easier way to accomplish that

View:

<table class="table table-hover">

<tr>
    <th>Nom Organisation</th>
    <th>Site Web</th>
</tr>

<% @organismes.each do |organisme| %>
    <tr>
        <td><%= organisme.nom_organisation %></td>
        <td><%= organisme.site_web %></td>
        <td><%= link_to 'Show', organismereferent_path(organisme), class: 'btn btn-info' %></td>
        <td><%= link_to 'Edit', edit_organismereferent_path(organisme), class: 'btn btn-warning' %></td>

        <% if organisme.active == false %>

        <td><%= link_to 'Activate', organismereferent_path(organisme), class: 'btn btn-danger',
                method: :activate,
                data: { confirm: 'Are you sure?' } %></td>
        <% else %>

        <td><%= link_to 'Deactivate', organismereferent_path(organisme), class: 'btn btn-danger',
                method: :deactivate,
                data: { confirm: 'Are you sure?' } %></td>

        <% end %>

    </tr>
<% end %>
</table>

Controller:

def index
  @organismes = Organismereferent.all
end

def deactivate
  @organisme = Organismereferent.find(params[:id])
  @organisme.active = false
end

def activate
  @organisme = Organismereferent.find(params[:id])
  @organisme.active = true
end

If you need any more information I will be glad too add it.

Routes:

Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get 'master/index'

resources :organismereferents

# Notre page principal
root 'master#index'
end

enter image description here

2
  • Could you post your routes.rb file? Also, from the command line, type rake routes and post the result. Commented May 16, 2017 at 22:40
  • @moveson Added it. Commented May 16, 2017 at 22:44

1 Answer 1

1

We have a few problems here.

First, in your views, the link_to helper method parameter doesn't want to know the name of your controller action. It wants to know the http method to use. For the actions you are doing, I think the most appropriate would be PATCH. So change those lines to method: :patch.

Second, you need to update your routes.rb file to include the routes you need. You can do that by replacing resources :organismereferents with the following:

resources :organismereferents do
  member { patch :activate }
  member { patch :deactivate }
end

Using the member designation indicates to Rails that you want to include an :id param in the URL. Now when you run rake routes you should see, in addition to what you already have, the following:

activate_organismereferents   PATCH  /organismereferents/:id/activate(.:format)   organismereferents#activate
deactivate_organismereferents PATCH  /organismereferents/:id/deactivate(.:format) organismereferents#deactivate

Third, you need to fix your path helpers. The first column returned by rake routes tells you the name of the Rails helper path that you can use in your link_to helpers. So in your Activate link, you'll need to change organismereferent_path(organisme) to activate_organismereferent_path(organisme), and in your Deactivate link, use deactivate_organismereferent_path(organisme).

Finally, in your controller actions, you are not saving the records after you change the boolean value. You will need to call @organisme.save to persist the change. Or if you would prefer to make the change and save it in a single line, you can use @organisme.update(active: true) and @organisme.update(active: false).

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

1 Comment

Thank you so much it works perfectly and now I understand a little bit more how things work in rails :) Is there any way of making the page refresh itself after the changes? Forget it I added redirect_to organismereferents_path and it does the trick. Thanks so much for your help :)

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.