Я хочу, чтобы в моем браузере поле URL-адрес, как это:Почему не изменился URL-адрес в поле браузера?
/posts/title_post instead /posts/id
Я написал:
routes.rb:
# added param: :title
resources :posts, param: :title do
resources :comments, only: [:new, :create]
resources :images do
resources :comments, only: [:new, :create]
end
resources :links do
resources :comments, only: [:new, :create]
end
resources :photos, only: [:new, :create,:destroy]
resources :songs, only: [:new, :create, :destroy]
end
расслоение Exec рейк маршруты:
/posts/new(.:format) posts#new
/posts/:title/edit(.:format) posts#edit
/posts/:title(.:format) posts#show
/posts/:title(.:format) posts#update
/posts/:title(.:format) posts#update
/posts/:title(.:format) posts#destroy
post_controller.rb:
def set_post
@post = Post.find(params[:title])
end
Блог работает, но Ссылка на пост/5 (к примеру). Как сделать url как: post/my_post_title?
class PostsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :show]
respond_to :html, :json, :rss, :atom
def index
if params[:search].blank?
@posts = Post.includes(:comments, :photos).all
else
@search = Post.search do
fulltext params[:search]
end
@posts = @search.results
end
respond_with(@posts)
end
def show
set_post
end
def new
@post = Post.new
end
def edit
set_post
end
def create
@post = Post.new(post_params)
# responder gem doc
@post.errors.add(:base, :invalid) unless @post.save#test
respond_with(@post)
end
def update
set_post
if @post.valid?
@post.update(post_params)
else
# responder gem doc
@post.errors.add(:base, :invalid)
end
respond_with(@post)
end
def destroy
set_post
@post.destroy
respond_with(@post)
end
def feed
@posts = Post.all.reverse
respond_with(@posts)
end
def archive
@posts_by_year = Post.limit(300).all.order("created_at DESC").
group_by {|post| post.created_at.beginning_of_year}
end
private
def set_post
@post = Post.find(params[:title])
end
def post_params
params.require(:post).permit(:title, :content)
end
end
Все маршруты:
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# feed GET /feed(.:format) posts#feed
# archive GET /archive(.:format) posts#archive
# new_user_session GET /users/sign_in(.:format) devise/sessions#new
# user_session POST /users/sign_in(.:format) devise/sessions#create
# destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
# user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) callbacks#passthru
# user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) callbacks#facebook
# user_password POST /users/password(.:format) devise/passwords#create
# new_user_password GET /users/password/new(.:format) devise/passwords#new
# edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
# PATCH /users/password(.:format) devise/passwords#update
# PUT /users/password(.:format) devise/passwords#update
# cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
# user_registration POST /users(.:format) devise/registrations#create
# new_user_registration GET /users/sign_up(.:format) devise/registrations#new
# edit_user_registration GET /users/edit(.:format) devise/registrations#edit
# PATCH /users(.:format) devise/registrations#update
# PUT /users(.:format) devise/registrations#update
# DELETE /users(.:format) devise/registrations#destroy
# root GET / posts#index
# post_comments POST /posts/:post_title/comments(.:format) comments#create
# new_post_comment GET /posts/:post_title/comments/new(.:format) comments#new
# post_image_comments POST /posts/:post_title/images/:image_id/comments(.:format) comments#create
# new_post_image_comment GET /posts/:post_title/images/:image_id/comments/new(.:format) comments#new
# post_images GET /posts/:post_title/images(.:format) images#index
# POST /posts/:post_title/images(.:format) images#create
# new_post_image GET /posts/:post_title/images/new(.:format) images#new
# edit_post_image GET /posts/:post_title/images/:id/edit(.:format) images#edit
# post_image GET /posts/:post_title/images/:id(.:format) images#show
# PATCH /posts/:post_title/images/:id(.:format) images#update
# PUT /posts/:post_title/images/:id(.:format) images#update
# DELETE /posts/:post_title/images/:id(.:format) images#destroy
# post_link_comments POST /posts/:post_title/links/:link_id/comments(.:format) comments#create
# new_post_link_comment GET /posts/:post_title/links/:link_id/comments/new(.:format) comments#new
# post_links GET /posts/:post_title/links(.:format) links#index
# POST /posts/:post_title/links(.:format) links#create
# new_post_link GET /posts/:post_title/links/new(.:format) links#new
# edit_post_link GET /posts/:post_title/links/:id/edit(.:format) links#edit
# post_link GET /posts/:post_title/links/:id(.:format) links#show
# PATCH /posts/:post_title/links/:id(.:format) links#update
# PUT /posts/:post_title/links/:id(.:format) links#update
# DELETE /posts/:post_title/links/:id(.:format) links#destroy
# post_photos POST /posts/:post_title/photos(.:format) photos#create
# new_post_photo GET /posts/:post_title/photos/new(.:format) photos#new
# post_photo DELETE /posts/:post_title/photos/:id(.:format) photos#destroy
# post_songs POST /posts/:post_title/songs(.:format) songs#create
# new_post_song GET /posts/:post_title/songs/new(.:format) songs#new
# post_song DELETE /posts/:post_title/songs/:id(.:format) songs#destroy
# posts GET /posts(.:format) posts#index
# POST /posts(.:format) posts#create
# new_post GET /posts/new(.:format) posts#new
# edit_post GET /posts/:title/edit(.:format) posts#edit
# post GET /posts/:title(.:format) posts#show
# PATCH /posts/:title(.:format) posts#update
# PUT /posts/:title(.:format) posts#update
# DELETE /posts/:title(.:format) posts#destroy
#
Я обновил свой ответ. Пожалуйста, смотрите. –