I want to build a filter with the help of scope in ruby on rails.
In my controller, I have the lines:
def index
@carpools = Carpool.paginate(page: params[:page], per_page: 5)
@carpools = @carpools.filter_by_locationFrom(params[:carpool_from]) if params[:carpool_from].present?
end
And in my model, I have the lines:
scope :filter_by_locationFrom, -> (locationFrom) { where locationFrom: locationFrom }
And in my view (index.html.erb), I want to have a form to type the value of locationFrom, so that the controller will know the params: locationFrom exists and only select the specified data (with the locationFrom value)from the database.
I have tried several form format but they all got some issues. How can I make such form?
Here is the form I tried:
<%= form_with(model: @carpool, method: "POST") do |f| %>
<div class="form-group row">
<%= f.label :locationFrom, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= f.text_field :carpool_from, class: "form-control shadow rounded", placeholder: "From" %>
</div>
</div>
<div class="form-group row justify-content-center">
<%= f.submit("filter", class: "btn btn-outline-light btn-lg") %>
</div>
<% end %>
This gives 400 bad request, saying that:
ActionController::ParameterMissing (param is missing or the value is empty: carpool):
app/controllers/carpools_controller.rb:20:in `create'
But my form is inside 'index' page, I don't know why it refers to 'create'
Thanks in advance!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…