Rails MVC 和 CRUD(10)
2023-03-20 14:48:57 时间
再次加载
可以成功显示了
列出所有文章
[root@h202 blog]# vim app/controllers/articles_controller.rb
[root@h202 blog]# cat app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def new
end
def create
# render plain: params[:article].inspect
# @article = Article.new(params[:article])
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def show
@article = Article.find(params[:id])
end
def index
@articles = Article.all
end
private
def article_params
params.require(:article).permit(:title,:text)
end
end
[root@h202 blog]# vim app/views/articles/index.html.erb
[root@h202 blog]# cat app/views/articles/index.html.erb
<h1>Listing articles</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
</tr>
<% end %>
</table>
[root@h202 blog]#
访问 /articles
添加链接
[root@h202 blog]# vim app/views/articles/index.html.erb
[root@h202 blog]# cat app/views/articles/index.html.erb
<h1>Link test!!!!</h1>
<%= link_to 'My Blog', controller: 'articles' %>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
</tr>
<% end %>
</table>
[root@h202 blog]# vim app/views/articles/show.html.erb
[root@h202 blog]# cat app/views/articles/show.html.erb
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<%= link_to 'Back', articles_path %>
[root@h202 blog]# vim app/views/articles/new.html.erb
[root@h202 blog]# cat app/views/articles/new.html.erb
<h1>Test blog http://soft.dog/</h1>
<%= form_for :article, url: articles_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
[root@h202 blog]#
相关文章
- selenium实战爬取股票
- Serverless 技术思考点滴
- springsecurity配合token进行权限控制
- spring整合mybatis各个配置文件
- ssm框架的各项配置的基本模板
- 机器阅读理解(Neural Machine Reading Comprehension)综述,相关方法及未来趋势
- 基础DDOS命令
- 力扣删除排序数组中的重复项
- 智能化+容错服务器是边缘计算场景下的最佳拍档
- 如何构建 “先发制人”的远程管控系统(--内存篇章)
- Kafka Magic-Kafka开源可视化工具
- DevOps 文化篇:亚马逊领导力原则
- K8s Clinic:如何安全高效地运行 K8s
- Docker-compose 工具解析
- 服务器性能监控:服务器监控的最佳实践
- 防止 DDOS 攻击的7个技巧
- 一文带你入门 Kubernetes 世界
- 时空质的外延部份涉及到其数值测度问题
- linux远程复制文件命令小总结
- 2021-11-20:一场电影开始和结束时间可以用一个小数组来表