मैं क्षमता के साथ करने के लिए ब्लॉग \ खबर आवेदन लागू करना चाहते हैं: example.com/2012/
रेल दिनांक के आधार पर 3.2 अनुकूल यूआरएल मार्ग
example.com/
- जड़ में सभी पोस्ट दिखाने सभी पोस्ट कुछ वर्ष और माह का जवाब दे:
example.com/2012/07/slug-of-the-post
example.com/2012/07/
तो मैं routes.rb
फ़ाइल के लिए एक mockup बनाया है:
# GET /?page=1
root :to => "posts#index"
match "/posts" => redirect("/")
match "/posts/" => redirect("/")
# Get /posts/2012/?page=1
match "/posts/:year", :to => "posts#index",
:constraints => { :year => /\d{4}/ }
# Get /posts/2012/07/?page=1
match "/posts/:year/:month", :to => "posts#index",
:constraints => { :year => /\d{4}/, :month => /\d{1,2}/ }
# Get /posts/2012/07/slug-of-the-post
match "/posts/:year/:month/:slug", :to => "posts#show", :as => :post,
:constraints => { :year => /\d{4}/, :month => /\d{1,2}/, :slug => /[a-z0-9\-]+/ }
तो मैं index
कार्रवाई में पैरामीटर साथ काम करना चाहिए और सिर्फ show
कार्रवाई में स्लग द्वारा पोस्ट प्राप्त (देखना हो तारीख corect है एक विकल्प है):
def to_param
"#{created_at.year}/#{created_at.month}/#{slug}"
end
: # GET /posts?page=1
def index
#render :text => "posts#index<br/><br/>#{params.to_s}"
@posts = Post.order('created_at DESC').page(params[:page])
# sould be more complicated in future
end
# GET /posts/2012/07/19/slug
def show
#render :text => "posts#show<br/><br/>#{params.to_s}"
@post = Post.find_by_slug(params[:slug])
end
इसके अलावा मैं अपने मॉडल के लिए to_param
को लागू करना
यह सब मैंने एपीआई/गाइड/एसओ में पूरी रात खोज से सीखा है।
लेकिन समस्या है विचित्र बातें नई के रूप में मेरे लिए happenning रखना रेल के लिए:
जब मैं
localhost/
पर जाते हैं, एप्लिकेशन टूट जाता है और कहता है कि यहshow
कार्रवाई लेकिन डेटाबेस में पहली वस्तु लागू किया था के रूप में प्राप्त किया गया था: (! इस प्रकार से) वर्ष:No route matches {:controller=>"posts", :action=>"show", :year=>#<Post id: 12, slug: "*", title: "*", content: "*", created_at: "2012-07-19 15:25:38", updated_at: "2012-07-19 15:25:38">}
जब मैं
01,235,localhost/posts/2012/07/cut-test
एक ही बात करने के लिए जाना होता है:No route matches {:controller=>"posts", :action=>"show", :year=>#<Post id: 12, slug: "*", title: "*", content: "*", created_at: "2012-07-19 15:25:38", updated_at: "2012-07-19 15:25:38">}
मुझे लगता है बहुत आसान है कि मैं नहीं किया है कुछ है कि वहाँ है, लेकिन मैं नहीं मिल सकता है कि यह क्या है।
वैसे भी, इस पोस्ट उपयोगी है जब यह, हल किया जाता है क्योंकि वहाँ सिर्फ तारीख और समान है, लेकिन उपयोगी नहीं प्रश्नों के बिना URL में मल के लिए समाधान कर रहे हैं हो जाएगा।
इस से पहले 2 'active_admin' के निर्देश हैं, इसलिए मुझे उन सभी संसाधनों की आवश्यकता नहीं है। – iEugene