Answer the question
In order to leave comments, you need to log in
How to correctly select data?
I’m back)
In general, the situation is as follows, I can’t figure out what the problem is, there are posts posts have a city, it is necessary that there is a choice by city, but something doesn’t work for me)
posts_controller.rb
def post_all
@posts = Post.all.paginate(page: params[:page])
@posts = Post.where(status: params[:status]).paginate(page: params[:page]) if params[:status].present?
@posts = Post.where(city_id: params[:city_id]).paginate(page: params[:page]) if params[:city_id].present?
end
<%= form_for all_post_path, :method => 'get' do |f| %>
<%= f.collection_select :city_id, City.all, :id, :name, :prompt => "Выберите город" %>
<%= render @posts %>
<%= submit_tag "Search", :name => nil %>
<% end %>
Answer the question
In order to leave comments, you need to log in
You have problems with logic, it must be something like this
def post_all
@posts = Post.all
@posts = @posts.where(status: params[:status]) if params[:status].present?
@posts = @posts.where(city_id: params[:city_id]) if params[:city_id].present?
@posts = @posts.paginate(page: params[:page])
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question