Home>
I have a search function implemented in rails. So I want to implement a search function with multiple words by white space, but it doesn't work.
The word search feature is now part of form_with, combined with other advanced searches.
In addition, since they use scope in the model, I do not know where to write multi-word processing.
When implemented before, it was only a word search, so we wrote a multiple word function in the controller, but this time using the scope in the model as described above, so I don't know how to assemble it. It is a state.
When using scope, how do I write code that is normally written in a controller such as a multi-word function?
Thanks for your professor.
<% = form_with (scope:: search, url: search_posts_path, method:: get, local: true) do | f |%>
Advanced search
<i></i><% = f.label: Add keyword%>
<% = f.text_field: name, value: @search_params [: name],
class: "search-text"%>
<i></i><% = f.label: price%>
<% = f.number_field: price_from, placeholder: "\ Min", value: @search_params [: price_from],
class: "search-number"%>~<% = f.number_field: price_to, placeholder: "\ Max", value: @search_params [: price_to],
class: "search-number"%>
<i></i><% = f.label: Category%>
<% = f.select: category, [["Men", "Men" "],
["Ladies", "Ladies"],
["Hobby", "Hobby"],
["Game", "Game"],
["Home appliance", "home appliance"],
["Interior", "Interior"],
["Goods", "Goods"],
["Sports", "Sports"],
["Other", "Other"]],{selected: @search_params [: category]},
{class: "search-category"}%>
<i></i><% = f.label: Product status%>
<% = f.check_box: status, {multiple: true, include_hidden: true},
'New', nil%>
<% = f.label: New product%>
<% = f.check_box: status, {multiple: true, include_hidden: true},
'No noticeable scratches or dirt', nil%>
<% = f.label: No noticeable scratches or dirt%>
<% = f.check_box: status, {multiple: true, include_hidden: true},
'Scratched and dirty', nil%>
<% = f.label: Scratches and dirt%>
<% = f.check_box: status, {multiple: true, include_hidden: true},
'Overall bad', nil%>
<% = f.label: Overall bad state%>
<% = link_to "Clear", search_posts_path%>
<% = f.submit 'Done', class: "search-button"%>
<% end%>
def search
@search_params = post_search_params
@posts = Post.search (@search_params) .order (sort)
end
private
def post_search_paramsparams.fetch (: search, {}). permit (: name,: price_from,: price_to,: category, status: [])
end
scope: search,->(search_params) do
return if search_params.blank?
name_like (search_params [: name])
.price_from (search_params [: price_from])
.price_to (search_params [: price_to])
.category_like (search_params [: category])
.status_like (search_params [: status])
end
scope: name_like,->(name) {where ('name LIKE?', "% # {name}%") if name.present?}
scope: price_from,->(from) {where ('?<= price', from) if from.present?}
scope: price_to,->(to) {where ('price<=?', to) if to.present?}
scope: category_like,->(category) {where (category: category) if category.present?}
scope: status_like,->(status) {where (status: status) if status.present?}
Add from here
def search
sort = params [: sort] || "created_at DESC"
@posts_else = Post.all.order (sort)
@search_params = post_search_params
# @ posts = Post.search (@search_params) .order (sort)
end
<% @ posts_else.each do | post |%>
<% = link_to post_path (post.id) do%>
\<% = converting_to_jpy (post.price)%>
<% = image_tag post.image.url%>
<% = truncate (post.name, length: 20)%>
<% end%>
<% end%>
<% end%>
-
Answer # 1
Related articles
- ruby - rails i want to search by multiple keywords
- ruby - use form_with in rails to specify multiple models and save values
- ruby on rails - i want to display search results
- ruby on rails - i want to implement a function that returns a line bot stamp
- ruby on rails - i want to implement a function for administrator users to register luggage for general users in rails
- ruby on rails - extract multiple specific columns from one activerecord and make them json
- ruby - how to implement the edit/update function of multiple tables when using form object in rails
- ruby - i want to set the search condition of rails where to the date of created_at (time is not included)
- ruby - rails i want to make the created_at time the same when creating multiple data at once
- ruby on rails - about multi-word search to multiple columns using ransack
- ruby on rails - i can't implement the rails like function
- ruby - i want to search for form information in rails and save the result
- ruby on rails - [rails] i'm implementing a search function using form_with, but i'm having trouble with the search results not b
- ruby on rails - how to implement a sort function by the value of the statistical column that aggregates the values of related
- [ruby on rails] i want to implement a follow function, but the like button is not displayed in the view
- ruby on rails - i want to implement a login function using devise
- ruby on rails - [rails] i want to implement a filtering function and display the corresponding items on the list screen
- ruby - how to implement the search function
- ruby on rails - search for related models
- ruby - [rails] implementation of search function
Trends
I don't think so hard. scope means that the (last) expression written there is added to the search expression.
scope: name_like,->(name) {where ('name LIKE?', "% # {name}%") if name.present?}
Please put in the {} of "I wrote a multi-word function in the controller".
Make the last expression a where () or where chain.
It is easier to write if if..end is less difficult.