#soulmate{background-color:#fafafa;border:1pxsolid#bbb;display:none;font:12px/14px"Helvetica Neue",Helvetica,Arial,sans-serif;font-weight:normal;list-style:none;margin:00010px;padding:0;position:absolute;text-shadow:000white;/* play around with the top and left properties for correct positioning */top:201px;left:460px;width:579px;z-index:1000;-webkit-border-radius:4px;-moz-border-radius:4px;-ms-border-radius:4px;-o-border-radius:4px;-khtml-border-radius:4px;border-radius:4px;-webkit-box-shadow:04px10pxrgba(0,0,0,0.2);-moz-box-shadow:04px10pxrgba(0,0,0,0.2);-khtml-box-shadow:04px10pxrgba(0,0,0,0.2);-ms-box-shadow:04px10pxrgba(0,0,0,0.2);-o-box-shadow:04px10pxrgba(0,0,0,0.2);box-shadow:04px10pxrgba(0,0,0,0.2);}#soulmate.soulmate-type-container{border-top:1pxsolid#ddd;}#soulmate.soulmate-type-container:first-child{border-top:none;}#soulmate.soulmate-type-suggestions{border-left:1pxsolid#ddd;float:right;list-style:none;padding:5px0;width:490px;letter-spacing:0.5px;}#soulmate.soulmate-suggestion{color:#111;cursor:pointer;font-weight:500;font-size:13px;padding:5px06px12px;text-decoration:none;}#soulmate.soulmate-suggestion.focus{color:white;margin-left:-1px;margin-right:-1px;padding-left:13px;position:relative;text-shadow:01px1px#32629b;z-index:1;-webkit-box-shadow:01px2pxrgba(0,0,0,0.3);-moz-box-shadow:01px2pxrgba(0,0,0,0.3);-khtml-box-shadow:01px2pxrgba(0,0,0,0.3);-ms-box-shadow:01px2pxrgba(0,0,0,0.3);-o-box-shadow:01px2pxrgba(0,0,0,0.3);box-shadow:01px2pxrgba(0,0,0,0.3);background:#545454;background:-moz-linear-gradient(top,#5454540,#444444100%);background:-webkit-gradient(linear,00,0100%,from(#545454),to(#444444));-ms-filter:"progid: DXImageTransform.Microsoft.gradient(startColorstr=#545454,endColorstr=#444444)";filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#545454,endColorstr=#444444);}#soulmate.soulmate-type{background-color:white;color:#333;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:11px;letter-spacing:0.5px;margin-right:490px;padding:10px10px010px;text-align:right;text-transform:capitalize;vertical-align:top;-webkit-border-radius:5px;-moz-border-radius:5px;-ms-border-radius:5px;-o-border-radius:5px;-khtml-border-radius:5px;border-radius:5px;zoom:1;}#soulmate.soulmate-type:before,#soulmate.soulmate-type:after{content:"";display:table;}#soulmate.soulmate-type:after{clear:both;}
最後,我們來裝飾查詢門面吧。
增加一個 controller
$ rails g controller home index
讓網站的 root 指向這個 home controller & index action
routes.rb
1
root`home#index`
接著設計一下 view 的畫面
index.html.erb
123456
<divclass="container"><h2>SearchNounsandVerbs</h2> <%= form_tag do %> <%= text_field_tag "search", nil, placeholder: "Search" %> <% end %></div>
varready=function(){varrender,select;render=function(term,data,type){returnterm;}select=function(term,data,type){// populate our search form with the autocomplete result$('#search').val(term);// hide our autocomplete results$('ul#soulmate').hide();// then redirect to the result's link // remember we have the link in the 'data' metadatareturnwindow.location.href=data.link}$('#search').soulmate({url:'/autocomplete/search',types:['nouns','verbs'],renderCallback:render,selectCallback:select,minQueryLength:2,maxResults:5})}// when our document is ready, call our ready function$(document).ready(ready);// if using turbolinks, listen to the page:load event and fire our ready function$(document).on('page:load',ready);
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require dataTables/jquery.dataTables
*= require_tree .
*/
# Gemfile
gem 'friendly_id', '~> 5.0.0' # Note: You MUST use 5.0.0 or greater for Rails 4.0+
>rails generate friendly_id
>rake db:migrate
# edit app/models/user.rb
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
end
# Change User.find to User.friendly.find in your controller
User.friendly.find(params[:id])
# If you're adding FriendlyId to an existing app and need
# to generate slugs for existing users, do this from the
# console, runner, or add a Rake task:
User.find_each(&:save)
考虑下面三条命令:
rails generate model user name:string
rails generate controller user –no-test-framework
rails generate integration_test user
其中比较容我困惑的是user这个单词, 出现在不同的场合可能有下面四种情况:
user
users
User
Users
上面的情况在railstutorial这本教程里面出现了至少3种, 在阅读的时候给我造成了很大的困惑. 我想, 到底什么才是最为推荐的? 如果不按照教程上面的例子做会不会有什么问题?