ROR request an action to get a range of items
How would I create an action that takes in parameters to limit search results? This would be out of my Posts model
Posts_controller:
def getOneThrougTen
params[firstNum] params[secondNum]
re开发者_Go百科turn range of some sort?
end
It's not entirely clear to me what you want to achieve, but if it is displaying search results in chunks, I'll advice you to use the will_paginate gem!
If you're looking for pagination type code, you should look at the new capabilities provided with Arel and ActiveRecord in Rails 3. That will allow you to do the following:
@posts = Post.all.limit(myLimit).offset(myOffset)
or
@posts = Post.where(["user_id = ?",myUserId]).limit(myLimit).offset(myOffset).order("created_at desc")
etc. etc. Very powerful stuff.
Once you learn the basics of how active record mimics sql calls passing the parameters becomes intuitive. For example this is how you would create a collection of @posts from your Post model based on a range from a particular column, order the results and limit the number returned.
You can watch the script/server output to see the actual SQL to help understand what is going on and why something doesn't go as planned.
@posts=Post.find(:all, :conditions=>["db_column_name between ? and ?", firstNum, secondNum],:order=>"db_column_name ASC", :limit=>10)
To actually send and read your params.. In your controller you can read params sent by the view and assign them to variables. For example you could do.
def getDateRangeandPassItOn
firstNum=params[:firstNum]
...
To actually send them from the view to your controller is different depending on if you are using a fully restful controller such as the one generated in rails scaffoldind. If all you want is to send them into a non restful method such as "getDateRangeandPassItOn" you would do something like this in your view.
<% form_for ({:controller => 'name', :action => "getDateRangeandPassItOn"}) do f%>
<%f.label :firstNum%>
<%f.textField :firstNum%>
<%f.submit "submit"%>
<%end%>
精彩评论