D
D
dexdev2014-12-19 00:31:34
Ruby on Rails
dexdev, 2014-12-19 00:31:34

How to separate two params attributes in rails?

I am doing a range filter on the advice of the community, I ran into a search problem, namely, I don’t understand how to split the params into 2
form attributes

<%= form_tag @sub_category, method: 'get' do |d| %>
<%= text_field_tag "diameter[]", nil, id: "ex2", :data => {'slider-min' => '1','slider-max' => '1000','slider-step' => '5','slider-value' => '[250,450]' } %>
<%= submit_tag 'Save' %>
<% end %>

Controller
def show
        @sub_category = SubCategory.friendly.find(params[:id])
        @items = @sub_category.items
        @items = @sub_category.items.where("size >= ?", params[:diameter].first) if params[:diameter].present?
        @items = @sub_category.items.where("size <= ?", params[:diameter].last) if params[:diameter].present?
        @items = @items
    end

what comes from the form
Processing by SubCategoriesController#show as HTML Parameters: {"utf8"=>"✓", "diameter"=>["21,451"], "commit"=>"Save"
this is how to split "diameter"= >["21,451"] on 21 and 451 I don't understand please tell me
first last each nothing helps

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2014-12-19
@AdilA

I don’t know what problem you are solving, why do you need a range, what the community advised ...
But if you need to "diameter"=>["21,451"]make 21 and 451 out of it, then:

vals = params[:diameter].first.split(',') # [21,451]
val1 = vals.first # 21
val2 = vals.last # 451

But in general, it seems that something is fundamentally wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question