Reputation: 457
I feel like I am really close to figuring this out. Iv tried so many different ways.
I want to create two selects
one for writing_type
and the other for sort_by
in the agreement
model, to filter the results. I think I need a different code in JS file for index.html
and possibly edit in my controller.
I found a piece of a solution to my problem, but i couldnt figure out the rest of what i need to make this work.
What i think i need to do: change the model variable to accept params[:writing_type]
and :sort_by
?
Index.html
<form accept-charset="UTF-8" action="/scribe_requests" method="get">
<input type='submit' value="go">
<select id="querySelct" name="writing_type">
<option value=":all" label="Writing Type">Writing Type</option>
<option value="College Applications" label="College Applications">College Applications</option>
<option value="College Essays" label="College Essays">College Essays</option>
<option value="Business Papers" label="Business Papers">Business Papers</option>
<option value="Resumes" label="Resumes">Resumes</option>
<option value="High-School Essays" label="High-School Essays">High-School Essays</option>
<option value="Scholarship Essays" label="Scholarship Essays">Scholarship Essays</option>
<option value="Language Translation" label="Language Translation">Language Translation</option>
</select>
<select id="landingSelect" name= "sort_by">
<option value="created_at desc" label="Sort By">Sort By</option>
<option value="created_at desc" label="Due Date(closer)">Due Date(closer)</option>
<option value="created_at asc" label="Due Date(further)">Due Date(further)</option>
</select>
</form>
controller:
def index
@agreements = Agreement.where("accepted = ?", false).where("due_date >= ?", DateTime.now).where("writing_type = ?", params[:writing_type]).order(params[:sort_by]).paginate(:page => params[:page], :per_page => 20)
end
Here is partial being called in index.html.erb
</div class="agreements-list">
<%= render 'agreements/lists', agreements: @agreements %>
</div>
The model agreements has columns that i want to sort it by and writing_types
that i want to filter with.
So the plan is to use the url and based off the url the list will change it uses &writing_type=1
for example to include at the end of the URL.
How can i add the script URL to the main URL at the top and have it refresh on its own? i can add ajax later!! Unless you have the ajax answer with it? thank you so much this is very important to me! thank you!
Upvotes: 0
Views: 56
Reputation: 4686
I feels like you are doing something straightforward in quite a hard way. You should be able to achive what you want without all the JS by doing it in a form as follows:
<form accept-charset="UTF-8" action="/agreements" method="get">
<input type='submit' value="go">
<select id="querySelct" name="writing_type">
<option value="0" label="Writing Type">Writing Type</option>
<option value="1" label="College Applications">College Applications</option>
<option value="2" label="College Essays">College Essays</option>
<option value="3" label="Business Papers">Business Papers</option>
<option value="4" label="Resumes">Resumes</option>
<option value="5" label="High-School Essays">High-School Essays</option>
<option value="6" label="Scholarship Essays">Scholarship Essays</option>
<option value="6" label="Language Translation">Language Translation</option>
</select>
<select id="landingSelect" name= "sort_by">
<option value="0" label="Sort By">Sort By</option>
<option value="1" label="Due Date(closer)">Due Date(closer)</option>
<option value="2" label="Due Date(further)">Due Date(further)</option>
</select>
</form>
By putting it in a form, with names against the selects, it should submit to the form path (I have guessed it is a get request to /agreements
which will route to your index action in the controller), with the parameters populated based on what option is selected
In your index action you should then be able to get the parameters by params[:writing_type]
and params[:sort_by]
Update:
To handle all writing types, do it in your index controller differently e.g.
if params[:writing_type] == "all" #or whatever option represents all
@agreements = Agreement.all
else
@agreements = Agreement.where(writing_type: params[:writing_type])
end
Upvotes: 1