AksharaDL
AksharaDL

Reputation: 63

Ruby on Rails search & Result on the same page error

Here the show.html displays a dropdown, which contain Roles. When we select the role we should able to get permission result in the same page

For that I used partial, but getting error as given in the image,

Without using partial when i tried to display in a separate display.html.erb file , I am getting proper result.

but i want to get result in same show.html.erb file.

Kindly give my some suggestions to attain the proper results

enter image description here

permission_controller

def display
  param = params[:role]
  id=param[:id]
  @roles = Role.includes(:permissions).all
  @uniq_controller = Role.joins(:permissions).where('roles.id=?',id).select('permissions.*').group_by { |p| p.description }

  redirect_to permissions_show_path
end

def show
  @permission = Permission.new
end

show.html.erb

<%= form_tag(:controller => "permissions", :action => "display") do %>
  <%= collection_select(:role, :id, Role.all, :id, :name) %>
  <button type="submit">search</button>
<% end %>

<th width="25px"> <%= "Controller" %></th>
<th width="25px"> <%= "Permissions" %></th>

<% @uniq_controller.each do |permission| %>
  <%= render partial:"display", locals:{permission:permission} %>
<% end %>

_display.html.erb

<thead>                      
  <th width="25px"> <%= permission.first.gsub("_"," ") %></th>                         
  <% permission.second.each do |cont| %>                              
    <tr>                           
      <th width="25px"><%= check_box_tag :permission_ids, {multiple: true}, 
 cont.id %><%= cont.name %></th>
    </tr>
  <% end %>
</thead>

Upvotes: 0

Views: 158

Answers (2)

Tarek N. Elsamni
Tarek N. Elsamni

Reputation: 1837

You have not defined @uniq_controller in the show action in permission_controller controller which triggers this error.

I'd recommend that you define a method called uniq_controller in permission_controller as follows:

def uniq_controller(id)
  Role.joins(:permissions).where('roles.id=?',id).select('permissions.*').group_by { |p| p.description }
end

and then make it available in your view as a helper method by adding this code to your permission_controller:

 helper_method :uniq_controller

So the code in permission_controller should be like:

helper_method :uniq_controller

def display
  @roles = Role.includes(:permissions).all

  redirect_to permissions_show_path
end

def show
  @permission = Permission.new
end

def uniq_controller(id)
  Role.joins(:permissions).where('roles.id=?',id).select('permissions.*').group_by { |p| p.description }
end

Then in your view show.html.erb replace:

@uniq_controller.each

with:

uniq_controller(params[:role][:id]).each

This should fix the error that you are getting and follows Rails practices, for more details about helper_method please refer to:

https://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method

One more recommendation is to rename permission_controller to permissions_controller to follow Rails resources/controller naming convention.

Upvotes: 1

Pavan
Pavan

Reputation: 33542

You haven't defined @uniq_controller in the show action which triggered that error. Just define it in the show action

def show
  @permission = Permission.new
  @uniq_controller = Role.joins(:permissions).where('roles.id=?',id).select('permissions.*').group_by { |p| p.description }
end

Upvotes: 1

Related Questions