We are currently implementing a search for compound conditions in ruby on railsThere are two column entries that attempt to display records that match that column.I changed the view and model, but I still haven't found any conditions for overlapping the two.I would appreciate it if you could let me know.
Controller
def index
@plants=Plan.includes(:guider).page(params[:page]).per(5).order("created_at DESC")
@plants=Plan.page(params[:page]).per(5).order("created_at DESC").search(params[:search])
end
Model
def self.search (search)
if search
Plan.where("(datetimes like?)AND(title like?), "%#{params[:search1]}%", "%#{params[:search2]}%")
else
Plan.all
end
end
<div class="david">
<%=form_tag plans_path,:method=>'get',:class=>'david'do%>
<p>strong>Place:</strong>/p>
<div style="width:250px;"placeholder="date">
<p><%=text_field_tag:datetimes, params[:search1]%>/p>
<p><%=text_field_tag: title, params[:search2]%>/p>
<%=submit_tag "Search", :name=>nil%>
</div>
<%end%>
</div>
def self.search(search)
if search
Plan.where("(datetimes like?)AND(title like?), "%#{params[:search1]}%", "%#{params[:search2]}%")
You cannot see params
in the model.If you want to rewrite it honestly,
def self.search(search1,search2)
if search1 & search2
Plan.where("(datetimes like?)AND(title like?), "%#{search1}%", "%#{search2}%")
Rewrite the caller to pass two arguments as well.
As @suzukis replied, params
cannot be used as it is in the model.
Also, in view,
<%=text_field_tag:datetimes, params[:search1]%>
If params[:search1]
is nil
, the generated html is
<input type="text" name="datetimes" id="datetimes">
will be
Because params
is called using a key that is not :datetimes
such as params[:search1]
or params[:search2]
, it seems that the parameters sent by the client are not handled properly.
def index
@plants=Plan.includes(:guider).page(params[:page]).per(5).order("created_at DESC")
@plants=Plan.page(params[:page]).per(5).order("created_at DESC").search(params[:search])
end
However, the second @plans=...
overrides the first @plans
, so it should be redundant.
I don't know what kind of schema it is, so I can't say whether or not the correct query has been issued.
To verify this, it is recommended to see what SQL is being issued by rails server
during the request response and to see if the controller dumps @plans
to get the appropriate data.
© 2024 OneMinuteCode. All rights reserved.