What's the Rails way of making super-minimal websites? (1 form --> results page)
I'm making a website with a simple form on the homepage. When the user submits the form, the application crawls a website and then presents the results on a new page.
- Domain.com has the search form (:method => "get")
- Domain.com/search/xxxxxx has the results of the search.
I'm having trouble thinking about Models开发者_C百科 and Controllers when I'm not working with obvious objects like Users, Posts, Threads, or ShoppingCart.
What's the Rails way of organizing such an application?
Try using sinatra
Your site should look like this. Rails is too heavy for your requirements.
get '/' do
erb :form, layout => :layout
end
get '/search/:key_word' do
# use params[:key_word] to do what u want
end
You do not need to use a model for every controller. In this case i would use a single SearchController
rails g controller Search index
add this to the routes:
match '/search/:keyword' => 'search#index'
root :to => 'search#index'
and in your controller you can write
class SearchController
def index
if params[:keyword]
# search for the keyword ...
else
# render the search-form
end
end
end
So it is pretty easy to do in rails.
Using rails in a case like this is useful if you have other parts of the site that need more functionality. Also working with the views is maybe easier. Otherwise it is now possible in rails 3 to load only the parts that you really need. So in this case you would want to not load ActiveRecord
.
An alternative approach is to use something simpler like sinatra.
Just use one controller, Search, and a model Searches. Then you can store every search in the DB and allow users to retrieve them, or create permanent urls for searches. You could use Nokogiri to do the web crawling.
精彩评论