How can I send a search query(URL params) using text_field in Rails 5?











up vote
1
down vote

favorite












In Task controller (action index) I have this line:



@tasks = Task.where("title LIKE '%#{params[:q]}%'")


In view I have form_tag like this:



= form_tag tasks_path(format: :js), method: :get do |f|
= text_field_tag :q, params[:q]
= submit_tag :Search


And it is works fine, output in terminal:



Started GET "/tasks.js?utf8=%E2%9C%93&q=example&commit=Search" for 127.0.0.1 at 2018-11-08 10:28:37 +0200
Processing by TasksController#index as JS
Parameters: {"utf8"=>"✓", "q"=>"example", "commit"=>"Search"}
Rendering welcome/index.html.haml
Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%example%')


But I need to use form_for not form_tag.
My form_for form:



= form_for tasks_path, method: :get, remote: true do |f|
= f.text_field :q, value: params[:q]
= f.submit :Search


Terminal output:



Started GET "/index?utf8=%E2%9C%93&%2Ftasks%5Bq%5D=fdvdfvdfv&commit=Search" for 127.0.0.1 at 2018-11-08 10:30:11 +0200
Processing by WelcomeController#index as JS
Parameters: {"utf8"=>"✓", "/tasks"=>{"q"=>"fdvdfvdfv"}, "commit"=>"Search"}
Rendering welcome/index.html.haml within layouts/application
Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%%')


And it is don't work, empty between '%%'.
Maybe you can help me.










share|improve this question







New contributor




wannabeaprogrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    In Task controller (action index) I have this line:



    @tasks = Task.where("title LIKE '%#{params[:q]}%'")


    In view I have form_tag like this:



    = form_tag tasks_path(format: :js), method: :get do |f|
    = text_field_tag :q, params[:q]
    = submit_tag :Search


    And it is works fine, output in terminal:



    Started GET "/tasks.js?utf8=%E2%9C%93&q=example&commit=Search" for 127.0.0.1 at 2018-11-08 10:28:37 +0200
    Processing by TasksController#index as JS
    Parameters: {"utf8"=>"✓", "q"=>"example", "commit"=>"Search"}
    Rendering welcome/index.html.haml
    Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%example%')


    But I need to use form_for not form_tag.
    My form_for form:



    = form_for tasks_path, method: :get, remote: true do |f|
    = f.text_field :q, value: params[:q]
    = f.submit :Search


    Terminal output:



    Started GET "/index?utf8=%E2%9C%93&%2Ftasks%5Bq%5D=fdvdfvdfv&commit=Search" for 127.0.0.1 at 2018-11-08 10:30:11 +0200
    Processing by WelcomeController#index as JS
    Parameters: {"utf8"=>"✓", "/tasks"=>{"q"=>"fdvdfvdfv"}, "commit"=>"Search"}
    Rendering welcome/index.html.haml within layouts/application
    Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%%')


    And it is don't work, empty between '%%'.
    Maybe you can help me.










    share|improve this question







    New contributor




    wannabeaprogrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      In Task controller (action index) I have this line:



      @tasks = Task.where("title LIKE '%#{params[:q]}%'")


      In view I have form_tag like this:



      = form_tag tasks_path(format: :js), method: :get do |f|
      = text_field_tag :q, params[:q]
      = submit_tag :Search


      And it is works fine, output in terminal:



      Started GET "/tasks.js?utf8=%E2%9C%93&q=example&commit=Search" for 127.0.0.1 at 2018-11-08 10:28:37 +0200
      Processing by TasksController#index as JS
      Parameters: {"utf8"=>"✓", "q"=>"example", "commit"=>"Search"}
      Rendering welcome/index.html.haml
      Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%example%')


      But I need to use form_for not form_tag.
      My form_for form:



      = form_for tasks_path, method: :get, remote: true do |f|
      = f.text_field :q, value: params[:q]
      = f.submit :Search


      Terminal output:



      Started GET "/index?utf8=%E2%9C%93&%2Ftasks%5Bq%5D=fdvdfvdfv&commit=Search" for 127.0.0.1 at 2018-11-08 10:30:11 +0200
      Processing by WelcomeController#index as JS
      Parameters: {"utf8"=>"✓", "/tasks"=>{"q"=>"fdvdfvdfv"}, "commit"=>"Search"}
      Rendering welcome/index.html.haml within layouts/application
      Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%%')


      And it is don't work, empty between '%%'.
      Maybe you can help me.










      share|improve this question







      New contributor




      wannabeaprogrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      In Task controller (action index) I have this line:



      @tasks = Task.where("title LIKE '%#{params[:q]}%'")


      In view I have form_tag like this:



      = form_tag tasks_path(format: :js), method: :get do |f|
      = text_field_tag :q, params[:q]
      = submit_tag :Search


      And it is works fine, output in terminal:



      Started GET "/tasks.js?utf8=%E2%9C%93&q=example&commit=Search" for 127.0.0.1 at 2018-11-08 10:28:37 +0200
      Processing by TasksController#index as JS
      Parameters: {"utf8"=>"✓", "q"=>"example", "commit"=>"Search"}
      Rendering welcome/index.html.haml
      Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%example%')


      But I need to use form_for not form_tag.
      My form_for form:



      = form_for tasks_path, method: :get, remote: true do |f|
      = f.text_field :q, value: params[:q]
      = f.submit :Search


      Terminal output:



      Started GET "/index?utf8=%E2%9C%93&%2Ftasks%5Bq%5D=fdvdfvdfv&commit=Search" for 127.0.0.1 at 2018-11-08 10:30:11 +0200
      Processing by WelcomeController#index as JS
      Parameters: {"utf8"=>"✓", "/tasks"=>{"q"=>"fdvdfvdfv"}, "commit"=>"Search"}
      Rendering welcome/index.html.haml within layouts/application
      Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%%')


      And it is don't work, empty between '%%'.
      Maybe you can help me.







      ruby-on-rails ruby-on-rails-4 ruby-on-rails-5






      share|improve this question







      New contributor




      wannabeaprogrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      wannabeaprogrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      wannabeaprogrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      wannabeaprogrammer

      61




      61




      New contributor




      wannabeaprogrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      wannabeaprogrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      wannabeaprogrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          You should use form_tag not form_for. form_for is used to create a form for a model object, which isn't your case.



          And to answer your question, when you look at the generated params in the log, you have "/tasks"=>{"q"=>"fdvdfvdfv"}. so params[:q] won't work in this case.



          My final answer, go with form_tag.






          share|improve this answer





















          • But form_tag don`t work with option "remote: true". And I need a remote form (not refreshing the page)
            – wannabeaprogrammer
            yesterday










          • @wannabeaprogrammer It does work with remote: true. See the docs apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag
            – Pavan
            yesterday












          • Just to add to this answer, you should look at using placeholders instead of interpolating params[:q] directly into your query - this will speed up your query and also safeguard it from SQL Injection. Section 2.2.1 in this guide will help -> guides.rubyonrails.org/active_record_querying.html
            – ThorTL67
            yesterday


















          up vote
          0
          down vote













          In Rails 5.1+ you should use form_with which replaces both form_for and form_tag.



          Without a model



          = form_with(url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q, params[:q]
          = f.submit_tag :Search


          Make sure you parameterize the SQL query to avoid a SQL injection vulnerability:



          @tasks = Task.where("title LIKE ?", "%#{params[:q]}%")


          Virtual model



          Or you can create a virtual model, which is a model without a database table:



          # app/models/seach_query.rb
          class SearchQuery
          include ActiveModel::Model
          attr_accessor :q
          end




          = form_with(model: (@search_query || SearchQuery.new) url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q
          = f.submit :Search




          class TasksController < ApplicationController
          # ...
          def index
          @tasks = Task.all
          if params[:search_query]
          @search_query = SearchQuery.new(params.fetch(:search_query).permit(:q))
          @tasks = @tasks.where('tasks.title LIKE ?', "%#{ @search_query.q }%")
          end
          end
          end


          The advantage of a virtual model is that you can use validations, localize fields with the I18n module etc and organize your code.






          share|improve this answer





















          • Ok, thanks. I wrote this code: = form_with(url: tasks_path(format: :js), method: :get, remote: true) do |f| = f.text_field :q, value: params[:q] = f.submit :Search - @tasks.each do |f| %li = f.title
            – wannabeaprogrammer
            yesterday










          • It sends a params but search doesn't work.
            – wannabeaprogrammer
            yesterday










          • Terminal ouput: Started GET "/tasks.js?utf8=%E2%9C%93&q=lalala&commit=Search" for 127.0.0.1 at 2018-11-08 17:55:04 +0200 Processing by TasksController#index as JS Parameters: {"utf8"=>"✓", "q"=>"lalala", "commit"=>"Search"} Rendering welcome/index.html.haml Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%lalala%') ↳ app/views/welcome/index.html.haml:33 Rendered welcome/index.html.haml (20.2ms) Completed 200 OK in 26ms (Views: 24.2ms | ActiveRecord: 0.4ms)
            – wannabeaprogrammer
            yesterday










          • "Does't work" is the most useless information in the world.
            – max
            yesterday










          • I don't see any changes on view.
            – wannabeaprogrammer
            yesterday











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          wannabeaprogrammer is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204030%2fhow-can-i-send-a-search-queryurl-params-using-text-field-in-rails-5%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          You should use form_tag not form_for. form_for is used to create a form for a model object, which isn't your case.



          And to answer your question, when you look at the generated params in the log, you have "/tasks"=>{"q"=>"fdvdfvdfv"}. so params[:q] won't work in this case.



          My final answer, go with form_tag.






          share|improve this answer





















          • But form_tag don`t work with option "remote: true". And I need a remote form (not refreshing the page)
            – wannabeaprogrammer
            yesterday










          • @wannabeaprogrammer It does work with remote: true. See the docs apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag
            – Pavan
            yesterday












          • Just to add to this answer, you should look at using placeholders instead of interpolating params[:q] directly into your query - this will speed up your query and also safeguard it from SQL Injection. Section 2.2.1 in this guide will help -> guides.rubyonrails.org/active_record_querying.html
            – ThorTL67
            yesterday















          up vote
          1
          down vote













          You should use form_tag not form_for. form_for is used to create a form for a model object, which isn't your case.



          And to answer your question, when you look at the generated params in the log, you have "/tasks"=>{"q"=>"fdvdfvdfv"}. so params[:q] won't work in this case.



          My final answer, go with form_tag.






          share|improve this answer





















          • But form_tag don`t work with option "remote: true". And I need a remote form (not refreshing the page)
            – wannabeaprogrammer
            yesterday










          • @wannabeaprogrammer It does work with remote: true. See the docs apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag
            – Pavan
            yesterday












          • Just to add to this answer, you should look at using placeholders instead of interpolating params[:q] directly into your query - this will speed up your query and also safeguard it from SQL Injection. Section 2.2.1 in this guide will help -> guides.rubyonrails.org/active_record_querying.html
            – ThorTL67
            yesterday













          up vote
          1
          down vote










          up vote
          1
          down vote









          You should use form_tag not form_for. form_for is used to create a form for a model object, which isn't your case.



          And to answer your question, when you look at the generated params in the log, you have "/tasks"=>{"q"=>"fdvdfvdfv"}. so params[:q] won't work in this case.



          My final answer, go with form_tag.






          share|improve this answer












          You should use form_tag not form_for. form_for is used to create a form for a model object, which isn't your case.



          And to answer your question, when you look at the generated params in the log, you have "/tasks"=>{"q"=>"fdvdfvdfv"}. so params[:q] won't work in this case.



          My final answer, go with form_tag.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Pavan

          29.4k62951




          29.4k62951












          • But form_tag don`t work with option "remote: true". And I need a remote form (not refreshing the page)
            – wannabeaprogrammer
            yesterday










          • @wannabeaprogrammer It does work with remote: true. See the docs apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag
            – Pavan
            yesterday












          • Just to add to this answer, you should look at using placeholders instead of interpolating params[:q] directly into your query - this will speed up your query and also safeguard it from SQL Injection. Section 2.2.1 in this guide will help -> guides.rubyonrails.org/active_record_querying.html
            – ThorTL67
            yesterday


















          • But form_tag don`t work with option "remote: true". And I need a remote form (not refreshing the page)
            – wannabeaprogrammer
            yesterday










          • @wannabeaprogrammer It does work with remote: true. See the docs apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag
            – Pavan
            yesterday












          • Just to add to this answer, you should look at using placeholders instead of interpolating params[:q] directly into your query - this will speed up your query and also safeguard it from SQL Injection. Section 2.2.1 in this guide will help -> guides.rubyonrails.org/active_record_querying.html
            – ThorTL67
            yesterday
















          But form_tag don`t work with option "remote: true". And I need a remote form (not refreshing the page)
          – wannabeaprogrammer
          yesterday




          But form_tag don`t work with option "remote: true". And I need a remote form (not refreshing the page)
          – wannabeaprogrammer
          yesterday












          @wannabeaprogrammer It does work with remote: true. See the docs apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag
          – Pavan
          yesterday






          @wannabeaprogrammer It does work with remote: true. See the docs apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag
          – Pavan
          yesterday














          Just to add to this answer, you should look at using placeholders instead of interpolating params[:q] directly into your query - this will speed up your query and also safeguard it from SQL Injection. Section 2.2.1 in this guide will help -> guides.rubyonrails.org/active_record_querying.html
          – ThorTL67
          yesterday




          Just to add to this answer, you should look at using placeholders instead of interpolating params[:q] directly into your query - this will speed up your query and also safeguard it from SQL Injection. Section 2.2.1 in this guide will help -> guides.rubyonrails.org/active_record_querying.html
          – ThorTL67
          yesterday












          up vote
          0
          down vote













          In Rails 5.1+ you should use form_with which replaces both form_for and form_tag.



          Without a model



          = form_with(url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q, params[:q]
          = f.submit_tag :Search


          Make sure you parameterize the SQL query to avoid a SQL injection vulnerability:



          @tasks = Task.where("title LIKE ?", "%#{params[:q]}%")


          Virtual model



          Or you can create a virtual model, which is a model without a database table:



          # app/models/seach_query.rb
          class SearchQuery
          include ActiveModel::Model
          attr_accessor :q
          end




          = form_with(model: (@search_query || SearchQuery.new) url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q
          = f.submit :Search




          class TasksController < ApplicationController
          # ...
          def index
          @tasks = Task.all
          if params[:search_query]
          @search_query = SearchQuery.new(params.fetch(:search_query).permit(:q))
          @tasks = @tasks.where('tasks.title LIKE ?', "%#{ @search_query.q }%")
          end
          end
          end


          The advantage of a virtual model is that you can use validations, localize fields with the I18n module etc and organize your code.






          share|improve this answer





















          • Ok, thanks. I wrote this code: = form_with(url: tasks_path(format: :js), method: :get, remote: true) do |f| = f.text_field :q, value: params[:q] = f.submit :Search - @tasks.each do |f| %li = f.title
            – wannabeaprogrammer
            yesterday










          • It sends a params but search doesn't work.
            – wannabeaprogrammer
            yesterday










          • Terminal ouput: Started GET "/tasks.js?utf8=%E2%9C%93&q=lalala&commit=Search" for 127.0.0.1 at 2018-11-08 17:55:04 +0200 Processing by TasksController#index as JS Parameters: {"utf8"=>"✓", "q"=>"lalala", "commit"=>"Search"} Rendering welcome/index.html.haml Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%lalala%') ↳ app/views/welcome/index.html.haml:33 Rendered welcome/index.html.haml (20.2ms) Completed 200 OK in 26ms (Views: 24.2ms | ActiveRecord: 0.4ms)
            – wannabeaprogrammer
            yesterday










          • "Does't work" is the most useless information in the world.
            – max
            yesterday










          • I don't see any changes on view.
            – wannabeaprogrammer
            yesterday















          up vote
          0
          down vote













          In Rails 5.1+ you should use form_with which replaces both form_for and form_tag.



          Without a model



          = form_with(url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q, params[:q]
          = f.submit_tag :Search


          Make sure you parameterize the SQL query to avoid a SQL injection vulnerability:



          @tasks = Task.where("title LIKE ?", "%#{params[:q]}%")


          Virtual model



          Or you can create a virtual model, which is a model without a database table:



          # app/models/seach_query.rb
          class SearchQuery
          include ActiveModel::Model
          attr_accessor :q
          end




          = form_with(model: (@search_query || SearchQuery.new) url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q
          = f.submit :Search




          class TasksController < ApplicationController
          # ...
          def index
          @tasks = Task.all
          if params[:search_query]
          @search_query = SearchQuery.new(params.fetch(:search_query).permit(:q))
          @tasks = @tasks.where('tasks.title LIKE ?', "%#{ @search_query.q }%")
          end
          end
          end


          The advantage of a virtual model is that you can use validations, localize fields with the I18n module etc and organize your code.






          share|improve this answer





















          • Ok, thanks. I wrote this code: = form_with(url: tasks_path(format: :js), method: :get, remote: true) do |f| = f.text_field :q, value: params[:q] = f.submit :Search - @tasks.each do |f| %li = f.title
            – wannabeaprogrammer
            yesterday










          • It sends a params but search doesn't work.
            – wannabeaprogrammer
            yesterday










          • Terminal ouput: Started GET "/tasks.js?utf8=%E2%9C%93&q=lalala&commit=Search" for 127.0.0.1 at 2018-11-08 17:55:04 +0200 Processing by TasksController#index as JS Parameters: {"utf8"=>"✓", "q"=>"lalala", "commit"=>"Search"} Rendering welcome/index.html.haml Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%lalala%') ↳ app/views/welcome/index.html.haml:33 Rendered welcome/index.html.haml (20.2ms) Completed 200 OK in 26ms (Views: 24.2ms | ActiveRecord: 0.4ms)
            – wannabeaprogrammer
            yesterday










          • "Does't work" is the most useless information in the world.
            – max
            yesterday










          • I don't see any changes on view.
            – wannabeaprogrammer
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote









          In Rails 5.1+ you should use form_with which replaces both form_for and form_tag.



          Without a model



          = form_with(url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q, params[:q]
          = f.submit_tag :Search


          Make sure you parameterize the SQL query to avoid a SQL injection vulnerability:



          @tasks = Task.where("title LIKE ?", "%#{params[:q]}%")


          Virtual model



          Or you can create a virtual model, which is a model without a database table:



          # app/models/seach_query.rb
          class SearchQuery
          include ActiveModel::Model
          attr_accessor :q
          end




          = form_with(model: (@search_query || SearchQuery.new) url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q
          = f.submit :Search




          class TasksController < ApplicationController
          # ...
          def index
          @tasks = Task.all
          if params[:search_query]
          @search_query = SearchQuery.new(params.fetch(:search_query).permit(:q))
          @tasks = @tasks.where('tasks.title LIKE ?', "%#{ @search_query.q }%")
          end
          end
          end


          The advantage of a virtual model is that you can use validations, localize fields with the I18n module etc and organize your code.






          share|improve this answer












          In Rails 5.1+ you should use form_with which replaces both form_for and form_tag.



          Without a model



          = form_with(url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q, params[:q]
          = f.submit_tag :Search


          Make sure you parameterize the SQL query to avoid a SQL injection vulnerability:



          @tasks = Task.where("title LIKE ?", "%#{params[:q]}%")


          Virtual model



          Or you can create a virtual model, which is a model without a database table:



          # app/models/seach_query.rb
          class SearchQuery
          include ActiveModel::Model
          attr_accessor :q
          end




          = form_with(model: (@search_query || SearchQuery.new) url: tasks_path(format: :js), method: :get) do |f|
          = f.text_field :q
          = f.submit :Search




          class TasksController < ApplicationController
          # ...
          def index
          @tasks = Task.all
          if params[:search_query]
          @search_query = SearchQuery.new(params.fetch(:search_query).permit(:q))
          @tasks = @tasks.where('tasks.title LIKE ?', "%#{ @search_query.q }%")
          end
          end
          end


          The advantage of a virtual model is that you can use validations, localize fields with the I18n module etc and organize your code.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          max

          43.4k856103




          43.4k856103












          • Ok, thanks. I wrote this code: = form_with(url: tasks_path(format: :js), method: :get, remote: true) do |f| = f.text_field :q, value: params[:q] = f.submit :Search - @tasks.each do |f| %li = f.title
            – wannabeaprogrammer
            yesterday










          • It sends a params but search doesn't work.
            – wannabeaprogrammer
            yesterday










          • Terminal ouput: Started GET "/tasks.js?utf8=%E2%9C%93&q=lalala&commit=Search" for 127.0.0.1 at 2018-11-08 17:55:04 +0200 Processing by TasksController#index as JS Parameters: {"utf8"=>"✓", "q"=>"lalala", "commit"=>"Search"} Rendering welcome/index.html.haml Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%lalala%') ↳ app/views/welcome/index.html.haml:33 Rendered welcome/index.html.haml (20.2ms) Completed 200 OK in 26ms (Views: 24.2ms | ActiveRecord: 0.4ms)
            – wannabeaprogrammer
            yesterday










          • "Does't work" is the most useless information in the world.
            – max
            yesterday










          • I don't see any changes on view.
            – wannabeaprogrammer
            yesterday


















          • Ok, thanks. I wrote this code: = form_with(url: tasks_path(format: :js), method: :get, remote: true) do |f| = f.text_field :q, value: params[:q] = f.submit :Search - @tasks.each do |f| %li = f.title
            – wannabeaprogrammer
            yesterday










          • It sends a params but search doesn't work.
            – wannabeaprogrammer
            yesterday










          • Terminal ouput: Started GET "/tasks.js?utf8=%E2%9C%93&q=lalala&commit=Search" for 127.0.0.1 at 2018-11-08 17:55:04 +0200 Processing by TasksController#index as JS Parameters: {"utf8"=>"✓", "q"=>"lalala", "commit"=>"Search"} Rendering welcome/index.html.haml Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%lalala%') ↳ app/views/welcome/index.html.haml:33 Rendered welcome/index.html.haml (20.2ms) Completed 200 OK in 26ms (Views: 24.2ms | ActiveRecord: 0.4ms)
            – wannabeaprogrammer
            yesterday










          • "Does't work" is the most useless information in the world.
            – max
            yesterday










          • I don't see any changes on view.
            – wannabeaprogrammer
            yesterday
















          Ok, thanks. I wrote this code: = form_with(url: tasks_path(format: :js), method: :get, remote: true) do |f| = f.text_field :q, value: params[:q] = f.submit :Search - @tasks.each do |f| %li = f.title
          – wannabeaprogrammer
          yesterday




          Ok, thanks. I wrote this code: = form_with(url: tasks_path(format: :js), method: :get, remote: true) do |f| = f.text_field :q, value: params[:q] = f.submit :Search - @tasks.each do |f| %li = f.title
          – wannabeaprogrammer
          yesterday












          It sends a params but search doesn't work.
          – wannabeaprogrammer
          yesterday




          It sends a params but search doesn't work.
          – wannabeaprogrammer
          yesterday












          Terminal ouput: Started GET "/tasks.js?utf8=%E2%9C%93&q=lalala&commit=Search" for 127.0.0.1 at 2018-11-08 17:55:04 +0200 Processing by TasksController#index as JS Parameters: {"utf8"=>"✓", "q"=>"lalala", "commit"=>"Search"} Rendering welcome/index.html.haml Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%lalala%') ↳ app/views/welcome/index.html.haml:33 Rendered welcome/index.html.haml (20.2ms) Completed 200 OK in 26ms (Views: 24.2ms | ActiveRecord: 0.4ms)
          – wannabeaprogrammer
          yesterday




          Terminal ouput: Started GET "/tasks.js?utf8=%E2%9C%93&q=lalala&commit=Search" for 127.0.0.1 at 2018-11-08 17:55:04 +0200 Processing by TasksController#index as JS Parameters: {"utf8"=>"✓", "q"=>"lalala", "commit"=>"Search"} Rendering welcome/index.html.haml Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE (title LIKE '%lalala%') ↳ app/views/welcome/index.html.haml:33 Rendered welcome/index.html.haml (20.2ms) Completed 200 OK in 26ms (Views: 24.2ms | ActiveRecord: 0.4ms)
          – wannabeaprogrammer
          yesterday












          "Does't work" is the most useless information in the world.
          – max
          yesterday




          "Does't work" is the most useless information in the world.
          – max
          yesterday












          I don't see any changes on view.
          – wannabeaprogrammer
          yesterday




          I don't see any changes on view.
          – wannabeaprogrammer
          yesterday










          wannabeaprogrammer is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          wannabeaprogrammer is a new contributor. Be nice, and check out our Code of Conduct.













          wannabeaprogrammer is a new contributor. Be nice, and check out our Code of Conduct.












          wannabeaprogrammer is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204030%2fhow-can-i-send-a-search-queryurl-params-using-text-field-in-rails-5%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          鏡平學校

          ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

          Why https connections are so slow when debugging (stepping over) in Java?