3

I was trying to update a User record (only the "closed" named column) from the user index's view. This is what I tried. But, it is not working. I used Devise gem to generate views and controllers. But, whatever, How can I update a records one particular value, directly from index(without going to edit's view). It would be very helpful, if someone would help me.

<% @users.each do |user| %>
                <tr>
                  <td> <%= user.last_name %></td>
                  <td> <%= user.telephone %></td>
                  <td> <%= form_for user do |f| %>
                    <% if user.closed %>
                      <%= f.hidden_field :closed, value: false %>
                      <%= f.submit "Activate" %>
                      <% else %>
                        <%= f.hidden_field :closed, value: true %>
                        <%= f.submit "Deactivate" %>
                      <% end %>
                  <% end %> </td>
                      <%= link_to 'Remove', user_destroy_path(user), method: :delete, data: { confirm: 'Are you sure?' } %></td>
                </tr>
              <% end %>

Controller:

def user_index
    if current_user.admin?
      if params[:user_search].present?
        @users = User.search_user(params[:user_search]).super_admin
      else
        @users = User.super_admin
      end
    else
      if params[:user_search].present?
        @users = User.search_user(params[:user_search]).admin
      else
        @users = User.admin
      end
    end
    respond_to do |format|
      format.html
      format.csv { send_data User.to_csv, type: 'text/csv', filename: "users.csv" }
    end
  end

here is my controller. Actually there is also something done to search the users

Tazwar Utshas
  • 921
  • 2
  • 17
  • 30

1 Answers1

2

In your controller, create two methods activate and deactivate.

In controller,

 def activate
    @user = User.find_by(:id=>params[:id])
    @user.update(:closed=>false)
    redirect_to #your path
  end

  def deactivate
    @user = User.find_by(:id=>params[:id])
    @user.update(:closed=>true)
    redirect_to #your path
  end

Set your routes as,

patch '#yourcontroller/:id/activate',to:'#yourcontroller#activate' , as: :activate
patch '#yourcontroller/:id/deactivate',to:'#yourcontroller#deactivate' ,as: :deactivate

and finally do changes in your view as,

<% @users.each do |user| %>
           <tr>
             <td> <%= user.last_name %></td>
             <td> <%= user.telephone %></td>
             <td> <%if user.closed?%>
                  <%= link_to 'activate',activate_path(id:user.id), method: :patch%>
                 <%else%>  
                  <%= link_to 'deactivate',deactivate_path(id:user.id), method: :patch%>
                  <% end -%> </td>
                  <%= link_to 'Remove', user_destroy_path(user), method: :delete, data: { confirm: 'Are you sure?' } %></td>
                </tr>
              <% end %>

Its helpful pls up-vote!

ray
  • 5,454
  • 1
  • 18
  • 40
Giridharan
  • 568
  • 4
  • 14
  • _Its helpful pls up-vote!_ ? Do not beg for votes ever in answer where you are not assured whether it helped OP or not also. – ray Jan 31 '19 at 05:59