I am working on a search form which should accept multiple params. But it currently accepts only one.
My Controller action:
def index
#halls = Hall.order(:name).page(params[:page]).per(9)
#city = City.all
#venue_type = VenueType.all
#event_type = EventType.all
if !params[:city].blank?
session[:city] = params[:city]
city = City.find_by(name: params[:city])
#halls = #halls.where(:city => city)
end
if !params[:venue_types].blank?
session[:venue_types] = params[:venue_types]
venue_types = VenueType.find_by(name: params[:venue_types])
#halls = #halls.where(:venue_types => venue_types)
end
if !params[:event_types].blank?
session[:event_types] = params[:event_types]
event_types = VenueType.find_by(name: params[:event_types])
#halls = #halls.where(:event_types => event_types)
end
end
My view:
<div class="search">
<%= form_tag(halls_path, :method => "get", class: "navbar-form", id: "search-form") do %>
<%= select_tag "city", options_from_collection_for_select(City.all, "name", "name") %>
<ul>
<% #venue_type.each do |venue| %>
<li>
<%= check_box_tag 'venue_type[]', venue.name -%>
<%= venue.name -%>
</li>
<% end %>
</ul>
<ul>
<% #event_type.each do |event| %>
<li>
<%= check_box_tag 'event_type[]', event.name -%>
<%= event.name -%>
</li>
<% end %>
</ul>
<button class="btn" type="submit">Search</button>
<% end %>
</div>
I bet the mistake is in the controller but as a newbie I can't find it.
Would be grateful for the answers.
Your view defined the check_box_tag with:
<%= check_box_tag 'venue_type[]', venue.name -%>
<%= venue.name -%>
<%= check_box_tag 'event_type[]', event.name -%>
<%= event.name -%>
As you see, your controller must get :value_type & :event_type from params instead of :value_types & :event_types. So you can change from either view or controller, it should work, but I recommend change from view like this:
<%= check_box_tag 'venue_types[]', venue.name -%>
<%= venue.name -%>
<%= check_box_tag 'event_types[]', event.name -%>
<%= event.name -%>
Related
I have an app with SalesOpportunities - beneath these there are SaleQualifiers (which belong_to SalesOpportunity) which fit into 4 stages ("Need", "Budget", "Fit", "Negotiation"). I'm displaying these 4 stages as tabs on the SalesOpportunity show page, and when the user clicks a tab it will call a custom controller action to select the relevant SaleQualifiers to display in the pane beneath. So far this works well, but I'm trying to ensure the tab updates with the selected stage as the "active" tab - and this isn't working. The pane holds the right info, but the tabs are not correct:
SalesOpportunity show Controller action:
def show
#sales_opportunity = SalesOpportunity.find(params[:id])
session[:sales_opportunity_id] = #sales_opportunity.id
#set the SaleQualifier stages to show on the tabs
#stages = Question.order(:id).pluck(:sale_stage).uniq
#stage = #sales_opportunity.sale_status
#questions = Question.where(sale_stage: #stage)
#find any answered sale_qualifiers in this sale_stage
#sale_qualifiers = SaleQualifier.find_by_sql(["SELECT sale_qualifiers.* FROM sale_qualifiers INNER JOIN questions ON questions.id = sale_qualifiers.question_id WHERE sales_opportunity_id = ? AND questions.sale_stage = ? ", #sales_opportunity.id, #stage])
#find some unanswered sale_qualifiers and build them
#sale_qualifier = SaleQualifier.new(sales_opportunity_id: params[#sales_opportunity.id])
#answer = #sale_qualifier.build_answer
#find the right question to render and link to the sale_qualifier
#question = Question.find(#sales_opportunity.next_question)
end
This action just ensures the right pane is set on first loading the SalesOpportunity show page.
Here's the custom action to find the right SaleQualifiers according to their actions:
def prospect_qualifiers
#sales_opportunity = SalesOpportunity.find_by(id: session[:sales_opportunity_id])
#stage = params[:sale_stage]
#questions = Question.where(sale_stage: #stage)
#stages = Question.order(:id).pluck(:sale_stage).uniq
#sale_qualifiers = SaleQualifier.find_by_sql(["SELECT sale_qualifiers.* FROM sale_qualifiers INNER JOIN questions ON questions.id = sale_qualifiers.question_id WHERE has_answer = 'true' AND sales_opportunity_id = ? AND questions.sale_stage = ? ", #sales_opportunity.id, #stage])
#when there's no sale_qualifiers from this sale_stage we can just select the first one according to the question and build that
if #sale_qualifiers.blank?
#question = Question.order(:id).where(sale_stage: #stage).first
#sale_qualifier = SaleQualifier.new(sales_opportunity_id: params[#sales_opportunity.id], question_id: #question.id)
#answer = #sale_qualifier.build_answer
render :modal_form
else
#when some of the sale_qualifiers of this sale_stage exist we'll need to be more specific about which question is next
#sale_qualifier = SaleQualifier.new(sales_opportunity_id: params[#sales_opportunity.id])
#answer = #sale_qualifier.build_answer
#find the right question to render and link to the sale_qualifier
#question = Question.find(#sales_opportunity.next_question)
render :modal_form
end
end
This takes the sale_stage as a parameter passed from the link to this action, and uses that to find the right SaleQualifiers.
The offending part of my view code is below:
<div id="qualifier-tabs">
<ul class="nav nav-tabs">
<%= render partial: 'shared/tabs', collection: #stages, as: :stage %>
</ul>
</div>
<div class="tab-content", id="qualifier_panel">
<%= render partial: 'shared/panel', collection: #stages, as: :stage %>
</div>
</div>
shared/tabs partial:
<% if stage == #stage %>
<li class="active"><%= link_to stage, prospect_qualifiers_sale_qualifiers_path(:sale_stage => stage), :remote => true, data: { disable_with: "Loading...", toggle: 'tab' } %></li>
<% else %>
<li><%= link_to stage, prospect_qualifiers_sale_qualifiers_path(:sale_stage => stage), :remote => true, data: { disable_with: "Loading...", toggle: 'tab' } %></li>
<% end %>
As you can see this compares the collection: #stages as: stage to the #stage variable passed from the controller action, determining whether they match - if so we make that the active tab.
The shared/panel partial:
<% if stage == #stage %>
<div class="tab-pane active" id='<%=stage %>'>
<table class="table table-striped display responsive no-wrap">
<thead>
<tr>
<th>Question</th>
<th>Answer</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<!-- if a sale_qualifier exists with an answer show the following -->
<% #sale_qualifiers.each do |qual| %>
<tr>
<td><%= qual.question.question_text %></td>
<% if qual.question.answer_type == "Datetime"%>
<td><%= qual.answer.answer_text.to_date.readable_inspect %></td>
<% elsif qual.question.answer_type == "Boolean" %>
<% if qual.answer.answer_text == 'true'%>
<td>Yes</td>
<% elsif qual.answer.answer_text == 'false' %>
<td>No</td>
<% end %>
<% else %>
<td><%= qual.answer.answer_text %></td>
<% end %>
<td><%= link_to('edit', edit_sale_qualifier_path(qual), class: "btn btn-sm btn-warning", data: { disable_with: "Loading...", dismiss: 'modal' }, :remote => true )%></td>
</tr>
<% end %>
<!-- if there's no sale_qualifier with the question id then build one -->
<%= form_tag('/sale_qualifiers', :id => 'new_sale_qualifier', :class => 'form', method: :post, remote: true, data: { model: "sale_qualifier" }) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.hidden_field :sales_opportunity_id, :value => #sales_opportunity.id %>
<%= ff.hidden_field :question_id, :value => #question.id %>
<tr>
<td><%= #question.question_text %></td>
<td>
<%= ff.fields_for :answer_attributes do |answer| %>
<div class="form-group">
<% if #question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text, :placeholder => "Enter your answer"%>
<% end %>
<% if #question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if #question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<span class="warning-block"></span>
<span class="help-block"></span>
</div>
<% end %>
</td>
<td>
<%= submit_tag "Submit", class: "btn btn-large btn-success", id: "sale_qualifier_submit", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="tab-pane" id='<%=stage %>'>
</div>
<% end %>
The pane does the same equivalence test and decides how to show the SaleQualifiers.
Here's the modal_form code:
$('#qualifier_tabs').html("<%= j render :partial => 'shared/tabs', collection: #stages, as: :stage %>");
$('#qualifier_panel').html("<%= j render :partial => 'shared/panel', collection: #stages, as: :stage %>");
The problem is that the pane shows the right SaleQualifiers but the active tab remains on "Need". If I put in <%= #stage %> and <%= stage %> outputs into the tab partial and also into the panel partial I get the reason - the tab panel always shows "Need" "Need" as the output from those erb snippets, whereas the pane panel shows whatever sale_stage is actually coming from my prospect_qualifiers action.
How can the prospect_qualifiers action (which calls the modal_form file and therefore renders both partials into the view) be passing different values for #stage and stage to these two partials when they're being rendered by the same controller action?
Edit - adding the show view
Per Chloe's request - here's the show view (or the relevant part of it) from the SalesOpportunity show page:
<div class="panel panel-default">
<div id="qualifier-tabs">
<ul class="nav nav-tabs">
<%= render partial: 'shared/tabs', collection: #stages, as: :stage %>
</ul>
</div>
<div class="tab-content", id="qualifier_panel">
<%= render partial: 'shared/panel', collection: #stages, as: :stage %>
</div>
</div>
I see now that you have <div id="qualifier-tabs"> in your view, but $('#qualifier_tabs') in your JQuery.
I have this on my view:
<div class='container'>
<div class='row upper_container'>
<div class='search_container'>
<%= form_tag deals_path, :method => :get, :class => 'navbar-form navbar-left' do %>
<div class='form-group'>
<%= text_field_tag :search, params[:search], class: 'form-control' %>
</div>
<%= submit_tag 'Search', :name => nil %>
<% end %>
</div>
</div>
<% #deals.each_with_index do |d, i| %>
<% if i % 3 == 0 %>
<div class='row middle_container'>
<% end %>
<div class='col-md-4'>
<div class='deal_container'>
<%= d.title %>
<img src='<%= d.photo %>', class='deal_img'>
</div>
</div>
<% if (i % 3 == 2) || (i == (#deals.length - 1)) %>
</div>
<% end %>
<% end %>
</div>
this in my controller:
class DealsController < ApplicationController
def index
# #deals = Deal.paginate(:page => params[:page])
#search = Deal.search do
fulltext params[:search]
end
#deals = #search.result
end
private
def deal_params
params.require(:deal).permit(:title)
end
end
and this in my model:
class Deal < ActiveRecord::Base
searchable do
text :title
end
end
when I want to do a seach by some word, like 'Treatment', the #deals variable, in the controller is null, but the param is being sent: Parameters: {"utf8"=>"✓", "search"=>"Treatment"}
any idea?
Try this:
query = params[:search]
#search = Deal.search do
fulltext query
end
#deals = #search.result
Please check this answer for details.
I have the following controller:
class Admin::ArticleChangeRequestsController < Admin::ApplicationController
before_action :set_order, only: :index
before_action :set_article_change_request, only: [:edit, :delete]
helper SortClassHelper
protected
def set_order
if params[:column].present?
#column = params[:column]
else
#column = 'created_at'
end
if params[:sort].present?
#sort = params[:sort]
else
#sort = 'desc'
end
#order = #column.to_s + ' ' + #sort.to_s
end
def set_article_change_request
#article_change_request = ArticleChangeRequest.find(params[:id])
end
def article_change_request_params
params[:article_change_request].permit(:title, :change, :article)
end
public
def index
#article_change_requests = ArticleChangeRequest.order(#order).page params[:page]
end
def edit
if request.patch?
if #article_change_request.update(article_change_request_params)
redirect_to :admin_article_change_requests, :flash => { success: t(:article_change_request_updated) }
end
end
end
def delete
if request.post?
if #article_change_request.delete
flash[:success] = t(:article_change_request_deleted)
render :json => { :success => true }
else
render :json => { :success => false }
end
end
end
end
and have added:
accepts_nested_attributes_for :article
in my ArticleChangeRequest model. However whenever I hit Update it does not validate or save the article. Here is my Form:
<%= form_for #article_change_request, { :url => :admin_article_change_request_edit } do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<div>
<%= f.submit t(:update), class: 'submit' %>
</div>
<% end %>
<% if #article_change_request.errors.any? %>
<div class="message errormsg">
<p>
<%= t :there_is %> <%= pluralize #article_change_request.errors.count, t(:error) %>
</p>
</div>
<% end %>
<div class="container">
<%= f.label :title, t(:change_request_title) + ':' %>
<%= f.text_field :title, size: 40, class: 'text' %>
<% unless #article_change_request.errors[:title].blank? then %>
<span class="note error">
<% #article_change_request.errors[:title].each do |e| %>
<% if e == #article_change_request.errors[:title].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= f.label :change, t(:change_request_change) + ':' %>
<%= f.text_area :change, size: '100x5' %>
<% unless #article_change_request.errors[:change].blank? then %>
<span class="note error">
<% #article_change_request.errors[:change].each do |e| %>
<% if e == #article_change_request.errors[:change].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<hr>
<%= f.fields_for :article do |a| %>
<div class="container">
<%= a.label :category_id, t(:article_category) + ':' %>
<%= a.select :category_id, [''].concat(Category.select_options) %>
<% unless #article_change_request.article.errors[:category_id].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:category_id].each do |e| %>
<% if e == #article_change_request.article.errors[:category_id].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= a.label :title, t(:article_title) + ':' %>
<%= a.text_field :title, size: 40, class: 'text' %>
<% unless #article_change_request.article.errors[:title].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:title].each do |e| %>
<% if e == #article_change_request.article.errors[:title].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= a.label :slug, t(:article_slug) + ':' %>
<%= a.text_field :slug, size: 40, class: 'text slug' %>
<% unless #article_change_request.article.errors[:slug].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:slug].each do |e| %>
<% if e == #article_change_request.article.errors[:slug].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= a.label :article_type, t(:article_type) + ':' %>
<%= a.select :article_type, [''].concat(ArticleType.select_options) %>
<% unless #article_change_request.article.errors[:article_type].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:article_type].each do |e| %>
<% if e == #article_change_request.article.errors[:article_type].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div id="article_content_container" class="container" style="display: none;">
<%= a.label :content, t(:article_content) + ':' %>
<%= a.text_area :content, size: '100x40', class: 'wysiwyg' %>
<% unless #article_change_request.article.errors[:content].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:content].each do |e| %>
<% if e == #article_change_request.article.errors[:content].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div id="article_link_container" class="container" style="display: none;">
<%= a.label :link, t(:article_link) + ':' %>
<%= a.text_field :link, size: 40, class: 'text' %>
<% unless #article_change_request.article.errors[:link].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:link].each do |e| %>
<% if e == #article_change_request.article.errors[:link].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= a.label :summary, t(:article_summary) + ':' %>
<%= a.text_area :summary, size: '100x5' %>
<% unless #article_change_request.article.errors[:summary].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:summary].each do |e| %>
<% if e == #article_change_request.article.errors[:summary].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<% end %>
Can anyone tell me why its not validating or updating?
params[:article_change_request].permit(:title, :change,
:article_attributes => {:category_id, :title, :slug, :article_type})
I am trying to show edit link for group_message_comment on a group_message page only when it was posted by the session user.
Talking about group_messages, I managed to show edit links only when it is the session user.
<% if #isme %>
<%= link_to 'Edit', edit_group_message_path(#group_message) %>
<% end %>
On the other hand,
talking about group_messages_comments, I failed to show edit links. I have no idea about this. Could you help me?
☆show.html.erb(group_messages_controller)
<p><b>Post:</b></p>
<div class="each_message">
<%= image_tag #group_message.group.imageurl,:width => '20', :height => '25' %><%= "(" + #group_message.group.name + ")" %>
<p>
<%= 'Page:' + #group_message.page.to_s + '&' %><%= 'Line:' + #group_message.line.to_s %>
<%= #group_message.member.name %>
(<%= #group_message.created_at.strftime'%Y-%m-%d %H:%M' %>)
</p>
<div class="group_message">
<p class="message_content"><%= #group_message.content %></p>
</div><!--group_message-->
<br/>
<% if #isme %>
<%= link_to 'Edit', edit_group_message_path(#group_message) %>
<% end %>
</div><!--each_message-->
<hr/>
<b>Comments:</b>
<% if #group_message.group_message_comments.present? %>
<% #group_message.group_message_comments.each do |gmsc|%>
<div class="group_message_comment">
<p><%= gmsc.member ? gmsc.member.name : "unknown" %> (<%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %>)</p>
<%= gmsc.content %>
<p>
<% if #isme_comment %>
<%= link_to 'Edit', edit_group_message_comment_path(#group_message.group_message_comments) %>
<% end %>
</p>
</div> <!--group_message_comment-->
<br/>
<% end %><!-- each do -- >
<% else %>
<ul>
<li>no comment yet.</li>
</ul>
<% end %> <!--if -->
☆group_messages_controller
def show
if !checklogin? then return end
#group_message = GroupMessage.find(params[:id])
#isme = me? #group_message
#group_message_comment = GroupMessageComment.new
#group_message_comment = GroupMessage.find(params[:id]).group_message_comments.build
#isme_comment = me? #group_message_comment<!#########--maybe this is wrong-->
respond_to do |format|
format.html # show.html.erb
format.json { render json: #group_message }
end
end
☆application_controller
def me? obj = nil
id_num = obj !=nil ? obj.member_id : params[:id].to_i
if session[:user_id] == id_num then
return true
else
return false
end
end
I just added this and made it. Thank you for your attentions.
<% if #gmsc_member_id == gmsc.member.id %>
<%= link_to 'Edit', edit_group_message_comment_path(gmsc) %>
<% end %>
I am showing check boxes in view page and data will come from the database. Here is my code and the problem is that while looping if the same parent name came it shows same parent checkboxes two times.
<% #permission.each do |f| %>
<% if #controller_code != f.controller_code %>
<% #controller_code = f.controller_code %>
<%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false, :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
<br/>
<% #permission.each do |f| %>
<% if #controller_code == f.controller_code %>
<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false, :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %> <%= f.action_name %>
<br/>
<% end %>
<% end %>
<% end %>
I have also tried this way but its not working properly..
<% #permission.each do |f| %>
<% if #controller_code != f.controller_code %>
<% #controller_code = f.controller_code %>
<%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false, :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
<br/>
<% end %>
<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false, :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %> <%= f.action_name %>
<br/>
<% end %>
I dont know that i m giving exact answer for your question, i think this can help you,
<% #permission.each do |f| %>
<% if #controller_code != f.controller_code %>
<% #controller_code = f.controller_code %>
<%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false, :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
<% else %>
<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false, :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %>
<%= f.action_name %>
<% end %>
<% end %>
I found the solution of my question here it is in controller do following
#permission=Permission.all.where(active: 1)
if #permission.blank? == false
#permissions = Permission.all.where(active: 1).order_by(:controller_code => "asc")
else
#permissions = ""
end
And in view page do following
<% #permissions.each do |f| %>
<% if #controller_code != f.controller_code %>
<% #controller_code = f.controller_code %>
<%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false, :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
<br/>
<% #permissions.each do |f| %>
<% if #controller_code == f.controller_code %>
<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false, :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %> <%= f.action_name %>
<br/>
<% end %>
<% end %>
<% end %>
<% end %>