I have used Ransack Gem,Rails 4.
roles_controllers.rb
def index
#q = Role.ransack(params[:q])
#roles = #q.result(distinct: true)
end
This is instance method in model(role.rb)
def user_count
self.users.count
end
This is my html table header(Roles/index.html.erb)
<table class="table table-bordered table-framed rb-usr-tbl">
<thead>
<tr>
<th><%= sort_link(#q, :name) %></th>
<th>Description</th>
<th><%= sort_link(#q,:role_users_user_count) %></th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody class="role-index">
<% #roles.each do |role| %>
<tr>
<td><%= role.human_role %></td>
<td><%= role.description %></td>
<td><%= role.user_count %></td>
<td>
<%= link_to edit_role_path(role), :remote => true,:title => "Edit Role" do %>
<i class="icon-pencil7 rb-pencil"></i>
<% end %>
</td>
<td>
<%= link_to role, method: :delete,:title => "Delete Role", data: { confirm: 'Are you sure?' } do %>
<i class="icon-trash rb-trash"></i>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
There is relationship between user and role are One role has_many users.
Here i want to sort this count of users in asc and desc order but with this code it does not work.
Help me if you can.
Thanks
Ransack is very smart itself, try to change line
<th><%= sort_link(#q,:role_users_user_count) %></th>
to
<th><%= sort_link(#q, :users_count) %></th>
Explanation: In the variable #q you keep a Ransack query object on the model Role, if you call sort_link on #q it will apply users_count as "join users relation and order by users count".
I have a check-box inside a form.I need if user will checked this check-box suddenly the action will execute and it will check the check-box status whether it is checked or not using Rails 3.My form is given below.
paym.html.erb:
<%= form_for :add_payment,:url => {:action => 'add_payment' },remote: true do |f| %>
<table class="table table-bordered">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-1 col-sm-1">
<col class="col-md-3 col-sm-3">
<col class="col-md-3 col-sm-3">
<col class="col-md-4 col-sm-4">
</colgroup>
<thead>
<tr>
<th class="text-center"><input type="checkbox"></th>
<th class="text-center">Sl. No</th>
<th class="text-center">Date</th>
<th class="text-center">Receipt No.</th>
<th class="text-center">Amount</th>
</tr>
</thead>
<tbody>
<% #result.each do |r| %>
<tr>
<th class="text-center"><%= check_box_tag :check_value,nil,:id => "checkbox1-1" %></th>
<td class="text-center"><%= r.id %></td>
<td class="text-center"><%= r.c_date %></td>
<td class="text-center"><%= r.Receipt_No %></td>
<td class="text-center"><i class="fa fa-rupee"></i><%= r.v_amount %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
paym_controller.rb:
class PaymentsController < ApplicationController
def add_payment
end
end
Here i have a table with some value .I also need when user will checked that box 1-It will check whether check box is checked or not,2-If checked then it will get all table value corresponding to that check-box.Please help me.
<%= check_box_tag :check_value,nil,:id => "checkbox1-1" %>
You had pass nil value instead pass the object r like:
<%= check_box_tag :check_value, :value => r, :id => "checkbox1-1" %>
Now using jquery you can check if checkbox is checked then retrieve it's value. In form of value you will get particular object r and from that you can fetch your required data.
I hope this may helps you :)
I have a hidden field in my table.I need to access all array value into controller's action method in rails.Check my below table.
paymentresult.html.erb:
<%= form_for :add_payment,:url => {:action => 'add_payment' },remote: true do |f| %>
<table class="table table-bordered">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-1 col-sm-1">
<col class="col-md-3 col-sm-3">
<col class="col-md-3 col-sm-3">
<col class="col-md-4 col-sm-4">
</colgroup>
<thead>
<tr>
<th class="text-center"><input type="checkbox"></th>
<th class="text-center">Sl. No</th>
<th class="text-center">Date</th>
<th class="text-center">Receipt No.</th>
<th class="text-center">Amount</th>
</tr>
</thead>
<tbody>
<% #result.each do |r| %>
<%= hidden_field_tag 'infos[]', r %>
<tr>
<th class="text-center"><%= check_box_tag :check_value, 1, :id => "checkbox1-1" %></th>
<td class="text-center"><%= r.id %></td>
<td class="text-center"><%= r.c_date %></td>
<td class="text-center"><%= r.Receipt_No %></td>
<td class="text-center"><i class="fa fa-rupee"></i><%= r.v_amount %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit "Add to payment",:class => "btn btn-success",:id => "switch_car11" %>
<% end %>
payment_controller.rb
class PaymentsController < ApplicationController
def add_payment
if params[:commit]
params[:check_value] == '1' ? remember : forget
else
if params[:add_payment][:p_catagory]=="Cheque"
#chk=true
end
end
end
private
def remember
#reslt=params[:infos]
#add_payment=AddPayment.create(:p_catagory => params[:add_payment][:p_catagory],:paid_amount => "here i want to add the table's v_amount value",:total_claim => 1 )
end
end
add_payment.rb:
class AddPayment < ActiveRecord::Base
attr_accessible :chk_details, :chk_no, :p_catagory, :paid_amount, :total_claim
end
I need when user will click on submit button then all array value(i.e-#result) should fetch to add_payment method.I tried by using hidden field but failed to fetch all data.Here my requirement is after submitting the form all value of #resultshould fetch into controller method so that i can save some value from this in DB.
I know there are alot of questions on this, but after looking at about a dozen or so none of them appear to be the same problem. I have an "Action" model that I am just simply trying to edit and update. (fyi, this isn't a nested form, I'm not using devise, and this isn't an activeadmin problem...like virtually all other questions about this topic are). I'm getting
error:
unpermitted parameters: utf8, _method, authenticity_token when I do this.
Action params in Actions_controller:
def action_params
params.permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
IF I change it to:
params.require(:action).permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
than I get the below error (and I can't find any solution for this):
undefined method `permit' for "update":String
Actions Controller:
def edit
#action = Action.find(params[:id])
#actiontypes = Actiontype.all
#categories = Category.all
#lead_categories = #categories.where(lead: true)
#user = current_user
#reviewer = User.find(#action.reviewer_id) if #action.reviewed?
end
def update
#action = Action.find(params[:id])
if #action.update!(action_params)
binding.pry
flash[:success] = "Loop closed!"
redirect_to '/closingloop/actions?reviewed=false'
else
flash[:danger] = "Something Went Wrong! The loop was not closed!"
render :edit
end
end
def action_params
params.permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
View:
<%= form_for [:closingloop, #action] do |f| %>
<%= f.hidden_field :reviewer_id, :value => #user.id %>
<%= f.hidden_field :reviewed, :value => true %>
<table width="70%" align="center" class="table table-responsive">
<tr>
<th>Tracking Number</th>
<td><%= #action.company.tracking_phone_number %></td>
</tr>
<tr>
<th>Target Number</th>
<td><%= #action.company.phonenumber %></td>
</tr>
<tr>
<th>Opportunity Name</th>
<td><%= #action.opportunity_name %></td>
</tr>
<tr>
<th>Opportunity Address</th>
<td><%= #action.customer_address %></td>
</tr>
<tr>
<th>Opportunity Phone</th>
<td><%= #action.caller_phone_number %></td>
</tr>
<tr>
<th>Call Recording</th>
<td><%= audio_tag(#action.call_recording_link, controls: true) %></td>
</tr>
<tr>
<th>Duration</th>
<td><%= #action.duration %></td>
</tr>
<tr>
<th>Call Status</th>
<td><%= #action.call_status %></td>
</tr>
<tr>
<th>Date & Time</th>
<td><%= #action.created_at.to_formatted_s(:long) %></td>
</tr>
<% if #action.reviewed? %>
<tr id="row_reviewed_by">
<th>Reviewed By</th>
<td><%= #reviewer.first_name %> <%= #reviewer.last_name %></td>
</tr>
<% end %>
<tr><td colspan="2"> </td></tr>
<tr id="q_call_answered">
<th>Call Answered?</th>
<td>
<div class="radio-toolbar">
<%= f.radio_button :call_answered, true, class: 'call_answered_true' %>
<%= f.label :call_answered, "Yes" %>
<%= f.radio_button :call_answered, false, class: 'call_answered_false' %>
<%= f.label :call_answered, "No" %>
</div>
</td>
</tr>
<tr id="why_not_answered" style="display:none;">
<th>Why wasn't it answered?</th>
<td>
<%= f.select :why, options_for_select([["No Answer", "N"], ["Abandoned", "A"], ["After Business Hours", "H"]], #action.why), { include_blank: true } %>
</td>
</tr>
<tr id="q_opportunity" style="display:none;">
<th>Was it a opportunity?</th>
<td>
<div class="radio-toolbar">
<%= f.radio_button :is_customer, true, class: 'opportunity_true' %>
<%= f.label :is_customer, "Yes" %>
<%= f.radio_button :is_customer, false, class: 'opportunity_false' %>
<%= f.label :is_customer, "No" %>
</div>
</td>
</tr>
<tr id="opportunity_type" style="display:none;">
<th>Opportunity Type</th>
<td>
<%= f.select :actiontype_id, options_from_collection_for_select(#actiontypes, 'id', 'action_type', #action.actiontype_id), { include_blank: true } %>
</td>
</tr>
<tr id="reason_for_call" style="display:none;">
<th>Reason for Call</th>
<td><%= f.select :category_id, options_from_collection_for_select(#categories, 'id', 'reason', #action.category_id), { include_blank: true } %></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" id="save" class="btn btn-success" /></td>
</tr>
</table>
<% end %>
The main problem here is that if you have model named 'action', your params by default will be named in the same way, which collides with ActionController default params. The simplest solution is, I guess, renaming your model.
In my view I have the following:
<div class="subject edit">
<%= form_for(:subject, :url => {:action => 'update', :id => #subject.id}) do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<div class="form-buttons">
<%= submit_tag("Update Subject") %>
</div>
<% end %>
In the same folder I have created a partial (_form.html.erb). But for some reason it is not rendered.
_form.html.erb:
<table summary="Subject form fields">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visible?</th>
<td><%= f.text_field(:visible) %></td>
</tr>
I'm running rails 3.2.7. Can some see what I'm doing wrong?
Thanks!
A server reset fixed the issue. Doh!