Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

how to use form to set the values of params (for Scope) in Ruby on Rails?

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.5k users

...