I am working on a simple search functionality that is located in a user dashboard view (which also has its own dashboard controller). Now, the search function runs on a single model, and upon performing the action, it is supposed to go to the index page of that particular model. However, the index page does not get rendered after searching and it remains in the dashboard page even though the log says it redirects to the index page.
Here is the log:
Started GET "/detected_vehicles?utf8=%E2%9C%93&%3Aq%5Blocation_eq%5D=1&commit=Search" for 127.0.0.1 at 2019-05-02 10:33:08 +0800
Processing by DetectedVehiclesController#index as JS
Parameters: {"utf8"=>"✓", ":q"=>{"location_eq"=>"1"}, "commit"=>"Search"}
Rendering detected_vehicles/index.html.slim within layouts/application
Rendered shared/_flash.html.slim (3.1ms)
Rendered detected_vehicles/index.html.slim within layouts/application (80.9ms)
Rendered layouts/_navbar.html.slim (2.9ms)
Rendered layouts/_footer.html.slim (3.6ms)
Completed 200 OK in 326ms (Views: 271.7ms | ActiveRecord: 6.4ms)
Here is my form_with code:
= form_with url: detected_vehicles_path, method: :get, class: 'ui form' do |f|
.field
= select_tag(":q[location_eq]", options_from_collection_for_select(Camera.all, "id", "full_location"), class: 'form-control', id: 'detected_vehicle')
= f.submit 'Search', class: 'form-control'
I have tried doing the same thing using form_tag instead and it works as expected. Here is my code using form_tag:
= form_tag detected_vehicles_path, method: :get, class: 'ui form' do
.field
= select_tag(":q[location_eq]", options_from_collection_for_select(Camera.all, "id", "full_location"), class: 'form-control', id: 'detected_vehicle')
= submit_tag 'Search', class: 'form-control'
While I know that I can settle with the form_tag solution right now, I would like to find out what's wrong with how I constructed my form_with since it might become the standard soon for writing forms since form_tag and form_for are technically soft-deprecated as I have read.
If you look closely, when using form_with you are using JS for the request:
Processing by DetectedVehiclesController#index as JS
because form_with sets local: false by default:
:local - By default form submits are remote and unobtrusive XHRs. Disable remote submits with local: true.
So, add local: true to form_with and see what happens.
Related
According to Rails documentation
config.filter_parameters used for filtering out the parameters that
you don't want shown in the logs, such as passwords or credit card
numbers. By default, Rails filters out passwords by adding
Rails.application.config.filter_parameters += [:password] in
config/initializers/filter_parameter_logging.rb. Parameters filter
works by partial matching regular expression.
So, why when I submit the form below
<%= form_with model: #user, url: admin_user_path, method: :delete do %>
<%= label_tag :password, t('forms.password') %>
<%= text_field_tag :password, nil %>
<%= button_tag t('forms.save'), type: 'submit' %>
<% end %>
I can see my password in the log?
<ActionController::Parameters {"utf8"=>"✓", "_method"=>"delete", "authenticity_token"=>"r22P2Mi1xcWOjRHGogoFaDcOec9/FgkC9btCo66qmqaKG/zwzUkbUGtATsTKV19OOYK80VBf1h0CzFtoRltQOA==", "password"=>"x", "button"=>"", "controller"=>"admin/users", "action"=>"destroy", "id"=>"at-example-com"} permitted: false>
Shouldn't the password be [FILTERED]?
The piece of code you're showing isn't from a log:
<ActionController::Parameters {"utf8"=>"✓", "_method"=>"delete", "authenticity_token"=>"r22P2Mi1xcWOjRHGogoFaDcOec9/FgkC9btCo66qmqaKG/zwzUkbUGtATsTKV19OOYK80VBf1h0CzFtoRltQOA==", "password"=>"x", "button"=>"", "controller"=>"admin/users", "action"=>"destroy", "id"=>"at-example-com"} permitted: false>
That output from the command like puts(params). The option filter_parameters is about log file which placed under log directory. E.g. log/development.log
Here is a piece of log file:
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxQJeccoEATtyCFy1eV", "user"=>{"first_name"=>"Juggy Head", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create user"}
You have used text_field_tag (It's going to input type text) that's why it's showing value in the log you need to use password_field_tag (It's going to input type password) like below
<%= form_with model: #user, url: admin_user_path, method: :delete do %>
<%= label_tag :password, t('forms.password') %>
<%= password_field_tag :password, nil %>
<%= button_tag t('forms.save'), type: 'submit' %>
<% end %>
Rails API doc here
For instance, password filtering to use above code
Update
I completely agree with this answer Зелёный
I am using Rails 5.1 and want to include a search field in my website, so I have added a small form with "form_with". In my server log claims that all is rendered as expected but the page /products does not load anyway. Instead the page does not react to "Submit":
Rendered products/index.html.erb within layouts/application (24.5ms)
This is my form:
<%= form_with(url: products_path, method: 'get') do |form| %>
<%= form.label :q, 'Search for:' %>
<%= form.text_field :q, id: 'q', value: params[:q] %>
<%= form.submit 'Search' %>
<% end %>
And this is my complete server log, just in case it helps.
Started GET "/products?utf8=%E2%9C%93&q=x&commit=Search" for 127.0.0.1 at 2017-09-09 09:39:37 +0200
Processing by ProductsController#index as JS
Parameters: {"utf8"=>"✓", "q"=>"x", "commit"=>"Search"}
Rendering products/index.html.erb within layouts/application
Product Load (0.7ms) SELECT "products".* FROM "products"
Rendered collection of products/_product.html.erb [5 times] (2.8ms)
Rendered products/index.html.erb within layouts/application (24.5ms)
Completed 200 OK in 98ms (Views: 78.3ms | ActiveRecord: 0.7ms)
The search field is on a different page than products/index.html.erb. But nevertheless my server says that it is "rendered". Why am I not redirected to it then?
I'm trying to use the Rspotify gem (it's a wrapper for the Spotify API) to do a simple search for an artist -- basically something similar to what's outlined in this blog post.
The issue is that I get a different response when I'm running the search in my console vs. in my app via the UI. I should be getting the full data set on the UI like I do when I'm in the console.
Here's my form snippet:
<%= form_for :artist, url: artist_search_path, html: {class: "form-inline"} do |f| %>
<div class="form-group">
<%= f.text_field :artist_name, class: "form-control", placeholder: "Enter artist or group" %>
<%= f.submit "Search", class: "btn btn-success" %>
</div>
<% end %>
Two routes:
get 'profile', to: "pages#profile"
post 'artist_search', to: "artists#search"
Here's my artists controller snippet:
def search
if !params[:artist][:artist_name].blank?
#artists = RSpotify::Artist.search(params[:artist][:artist_name])
redirect_to profile_path
end
end
Here's a snippet of what I get when I submit an artist name on the UI and print out #artists in my logs:
Processing by ArtistsController#search as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"uzubTTE/bZvLBbU0MpY8lmLur1YPExnwdvXMXs882D7bm+fcUgTC8zRLizPh9Gp7eUjG2cGkNIGSk/Z+aZhrpg==", "artist"=>{"artist_name"=>"Drake"}, "commit"=>"Search"}
#<RSpotify::Artist:0x007faf454f4f60>
#<RSpotify::Artist:0x007faf454f4f10>
#<RSpotify::Artist:0x007faf454f4ec0>
# Bunch more of the above...
Redirected to https://website.com/profile
Completed 302 Found in 81ms (ActiveRecord: 0.0ms)
And I get a list of these when I run the search in the console:
[#<RSpotify::Artist:0x000000049f0bd8 #followers={"href"=>nil, "total"=>2645122}, #genres=["east coast hip hop", "hip hop", "pop rap", "rap", "southern hip hop", "trap music"], #images=[{"height"=>667, "url"=>"https://i.scdn.co/image/f58a98bc92cd2dc7020fc45a45eebb29380048ab", "width"=>1000}, {"height"=>427, "url"=>"https://i.scdn.co/image/9dd1bab34761764a125508534e443a9b245a9b56", "width"=>640}, {"height"=>133, "url"=>"https://i.scdn.co/image/620b2133ab3f20045c0d9669e706bbf11786cf78", "width"=>200}, {"height"=>43, "url"=>"https://i.scdn.co/image/5018ebe8a0d1060e6b39b2021132c082be1a913d", "width"=>64}], #name="JAY Z", #popularity=83, #top_tracks={}, #external_urls={"spotify"=>"https://open.spotify.com/artist/3nFkdlSjzX9mRTtwJOzDYB"}, #href="https://api.spotify.com/v1/artists/3nFkdlSjzX9mRTtwJOzDYB", #id="3nFkdlSjzX9mRTtwJOzDYB", #type="artist", #uri="spotify:artist:3nFkdlSjzX9mRTtwJOzDYB">]
This is my login form
<%= form_for Customer.new, url: {action: :login} do |f| %>
<%= f.text_field :username,placeholder: 'Username or Email' %>
<%= f.password_field :password,placeholder: 'Password' %>
<%= f.submit 'Login' %>
<% end %>
This is my controller
def login
username = params[:username]
password = params[:password]
unless username.blank? && password.blank?
#My code doesn't entering to this block
end
end
If i submit form it is just submitted it doesn't perform any action
Edit 1
This is what my console return when form submitted
Started POST "/auth/login" for 127.0.0.1 at 2013-09-08 08:23:05 +0530
Processing by AuthController#login as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9f7TKlIspKQwX7jMSzI7XGrabgJKvnzj8Ip0OLTDtW4=", "customer"=>{"username"=>"xxx", "password"=>"[FILTERED]"}, "commit"=>"Login"}
Rendered auth/login.html.erb within layouts/application (3.5ms)
Rendered layouts/_header.html.erb (0.4ms)
Completed 200 OK in 21ms (Views: 19.3ms | ActiveRecord: 0.0ms)
If you check your params hash you'll see that those keys don't exist. Instead, you'll see that params[:custpmer] does and that it contains those fields. This is just how rails builds the params in a form_for style. So try this instead:
username = params[:customer][:username]
password = params[:customer][:password]
I have simple form which allows users to filter search results according to location_id which is given in collection_select form's field. I want to render narrowed results if specific location is selected, and render all results if user selects default "Please select" field.
This code works good for separate locations, but not working if user selects "please select" field. params[:location_id].blank? in controller not working.
view
<%= form_tag #city %>
<%= collection_select 'location_id', nil, #city.locations, :id, :address, prompt: true %>
<%= submit_tag "sort", :name => nil, class: "btn btn-success travel-button", id: "hotels-radius-search-button" %>
<% end %>
controller
if params[:location_id].blank?
...render all results...
else
..render specific results...
end
Update
Server's response
Parameters when default "Please select" option is selected -- not working
Parameters: {"utf8"=>"✓", "search_radius"=>"6", "location_id"=>[""], "_"=>"1365350127863", "id"=>"75"}
Parameters when user select any option in menu -- succesful
Parameters: {"utf8"=>"✓", "search_radius"=>"6", "location_id"=>["811"], "_"=>"1365350127865", "id"=>"75"}
or
Parameters: {"utf8"=>"✓", "search_radius"=>"6", "location_id"=>["808"], "_"=>"1365350127865", "id"=>"75"}
etc
Posting my answer, since comments cannot be accepted :-)
collection_select was used and was sending an Array.
Therefore usage of select_tag was suggested.