So, I have a React-Component (search_alerts.es6.jsx) that looks like this
class SearchAlerts extends React.Component {
constructor(props) {
super(props);
console.log(this.props);
this.state = {
alertsArray: this.props.alerts,
zipCode: null,
bloodReq: null,
minCompensation: null,
maxCompensation: null,
requiredTime: null,
location: null,
bloodType: null,
keyWord: null
}
console.log(this.state.alertsArray);
}
........
and the ajax call that is responsible to call the get method looks like this
$.ajax({
data: JSON.stringify(stringit),
type: "GET",
url: `/findalerts/`,
contentType: "application/json",
dataType: "json",
success: function(data) {
console.log("Ajax Passed ");
console.log(data);
this.setState({
alertsArray: data
});
}.bind(this),
error: function(data) {
console.log("Ajax failed: ");
console.log(data);
}.bind(this)
});
in my routes.rb, I'm doing this
get '/findalerts', to: 'alerts#index' #Search Alerts
and in my alerts_controller
def index
@alerts = Alert.search(params[:keyword])
end
for the search, I'm doing this in alert.rb
def self.search(search)
@alerts = Alert.where("ZipCode LIKE :search ", search: "%#{search}%")
@alerts += Alert.where("compensation LIKE :search ", search: "%#{search}%")
@alerts += User.search(search)
end
Now if I pass the query in the url,like this,
http://localhost:3000/findalerts?keyword=117
it is returning for example all the alerts that has the zipcode starts from 117 as json, and the react component is fetching it and displaying it properly. My question is, how to get the search working when user types the keyword in the search form? I've tried other examples from stack overflow,it didn't work, I know I'm missing something, but don't know what. Thanks!