Learn Rails5.2 Routes摘录

Naming a Route

get ‘home/index‘, as: "different_name"

会得到prefix: different_name代替home_index, 这个是路径的名字。

get ‘home/ping‘, to: "home#pong"

to方法,定义到哪个控制器的哪个action。 这会改变默认的控制器的action。而控制器会指向view。



Paramter

通过浏览器输入的URI Pattern ,可以导向控制器的某个action

在router.rb中

resources :posts

get ‘:year(/:month(/:day))‘, to: ‘posts#index‘

Verb  URI Pattern               Controller#Action
GET    /:year(/:month(/:day))(.:format)		 posts#index 

解释:这里参数:year是必须的,括号里的是可选的。

http://localhost:3000/2018/01/01

Started GET "/2018/01/01" for 127.0.0.1 at 2018-06-18 09:52:25 +0800

Processing by PostsController#index as HTML

Parameters: {"year"=>"2018", "month"=>"01", "day"=>"01"}

Rendering posts/index.html.erb within layouts/application

Post Load (0.1ms)  SELECT "posts".* FROM "posts"

? app/views/posts/index.html.erb:16

Rendered posts/index.html.erb within layouts/application (1.8ms)

Completed 200 OK in 39ms (Views: 37.7ms | ActiveRecord: 0.1ms)

解释:在controller的index动作中, 你可以利用params[]来存取定义在URL中的值。

valid_date?方法,找不到出处?

def index

if Date.valid_date?(params[:year].to_i, params[:month].to_i, params[:day].to_i)

start_date = Date.parse("#{params[:day]}.#{params[:month]}.#{params[:year]}")

end_date = start_date

elsif Date.valid_date?(params[:year].to_i, params[:month].to_i, 1)

start_date = Date.parse("1.#{params[:month]}.#{params[:year]}")

end_date = start_date.end_of_month

elsif params[:year] && Date.valid_date?(params[:year].to_i, 1, 1)

start_date = Date.parse("1.1.#{params[:year]}")

end_date = start_date.end_of_year

end

if start_date && end_date

@posts = Post.where(published_on: start_date..end_date)

end

end



constraints(constraints={})

用于限制url

get ‘:year(/:month(/:day))‘, to: ‘posts#index‘, constraints: {year: /\d{4}/, month: /\d{2}/, day: /\d{2}/ }

也可以接受块:

constraints(id: /\d+\.\d+/) do

resources :posts

end

加入??后,id只能输入1.1或者10.11的格式,否则会报告Routing Error.



Redirect(*args, &block)方法

用于返回到其他path。设置比较复杂。

get ‘:year/:month/:day‘, to: redirect("/%{year}/0%{month}/0%{day}"), constraints: { year: /\d{4}/, month: /\d{1}/, day: /\d{1}/ }

还必须加上??,否则不能导航到控制器的index动作。

get ‘:year(/:month(/:day))‘, to: ‘posts#index‘, constraints: {year: /\d{4}/, month: /\d{2}/, day: /\d{2}/ }

我的理解:第一行代码是返回到第二行代码的路径,然后再指向控制器的index动作。


Nested  Resources

原文地址:https://www.cnblogs.com/chentianwei/p/9194755.html

时间: 2024-10-29 19:23:54

Learn Rails5.2 Routes摘录的相关文章

Learn Rails5.2- Scaffolding and REST

用generator建立一个手脚架 Representational State Transfer (REST).  具像的状态转存. https://en.wikipedia.org/wiki/Representational_state_transfer RESTful风格的简单的理解: 如何根据一个简单的法则来存取数据.法则包括CRUD的原理和对HTTP的明确定义. Learn Rails5.2- Scaffolding and REST 原文地址:https://www.cnblogs.

Learn Rails5.2- ActiveRecord: Migration , spring的使用(不兼容的解决办法)

偶然一次: 运行rails generate停止不动,网上查找答案,可能是bundle update 之后 spring 版本变化了,和正在运行的 spring 实例不兼容. Spring导致的同样的原因: rails g migration后,窗口显示了生成迁移文件,但文件树结构上没有显示这个文件. rails console停止不动,打不开控制台. 解决办法: 关闭spring, spring stop ??,如果谷歌上查找问题,中文描述问题找不到好的结果,改用英文描述问题,然后搜索. ht

Ruby on Rails Installation(Learn Rails5.2)

使用版本控制器的原因: 你没有系统根权限,所以你没有别的选择 你想要分开运行几个rails 系统 ,并且这几个rails有不同的Ruby版本.使用RVM就可以轻松做到. 没有什么新鲜的先安装xcode,再安装rvm->ruby->rails.安装homebrew->posalsql. 原文地址:https://www.cnblogs.com/chentianwei/p/9172365.html

Supporting Connected Routes to Subnet Zero

Supporting Connected Routes to Subnet Zero IOS allows the network engineer to tell a router to either allow addresses in the zero subnet or not. The motivation has to do with some older IP routing protocols that did not support the use of the zero su

[Angular2 Router] Configure Auxiliary Routes in the Angular 2 Router - What is the Difference Towards a Primary Route?

In this tutorial we are going to learn how we can can configure redirects in the angular 2 router configuration. We are also going to see again another example on how the order of the configuration might trip us. We are going to see the different bet

[Angular2 Router] Configuring a Home Route and Fallback Route - Learn An Essential Routing Concept

In this tutorial we are going to learn how to configure the Angular 2 router to cover some commonly used routing scenarios: what if the user goes to the root of the application manually ? Probably you would want some sort of home page to be displayed

自己喜欢的一些句子摘录-2018-03-04

自己喜欢的一些文章中的句子摘录... ================= 真实地生活,自有千钧之力将他们带出那段看上去艰苦的岁月.小时候也是同一个我,用一个下午的时间看蚂蚁搬家,等石头开花,小时候不期待结果,小时候哭笑都不打折===============有时候,优秀的人不愿意跟你合群,只是因为,他合的那个圈子里,不需要你.可是,如果我们不努力,那就等同于自我放弃,日后,又怎敢保证不会后悔当初那个胆怯的自己?===========一个人最大的勇敢,便是明知道自己跟别人的差距很大,但依旧鼓起勇气去

【Machine Learn】决策树案例:基于python的商品购买能力预测系统

决策树在商品购买能力预测案例中的算法实现 作者:白宁超 2016年12月24日22:05:42 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结合视频学习和书籍基础的笔记所得.本系列文章将采用理论结合实践方式编写.首先介绍机器学习和深度学习的范畴,然后介绍关于训练集.测试集等介绍.接着分别介绍机器学习常用算法,分别是监督学习之分类(决策树.临近取样.支持向量机.神经网络算法)监督学习之回归(线性回归.非线性回归)非监督学习(

TF.Learn

TF.Learn 手写文字识别 转载请注明作者:梦里风林Google Machine Learning Recipes 7官方中文博客 - 视频地址Github工程地址 https://github.com/ahangchen/GoogleML欢迎Star,也欢迎到Issue区讨论 mnist问题 计算机视觉领域的Hello world 给定55000个图片,处理成28*28的二维矩阵,矩阵中每个值表示一个像素点的灰度,作为feature 给定每张图片对应的字符,作为label,总共有10个la