This commit is contained in:
Xingyu Wang 2020-11-25 23:27:57 +08:00
parent b0225bd4f7
commit 05ba8b1d94
2 changed files with 95 additions and 99 deletions

View File

@ -1,99 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Day 1: a confusing Rails error message)
[#]: via: (https://jvns.ca/blog/2020/11/09/day-1--a-little-rails-/)
[#]: author: (Julia Evans https://jvns.ca/)
Day 1: a confusing Rails error message
======
Today I started an Recurse Center batch! I got to meet a few people, and started on a tiny fun Rails project. I think I wont talk too much about what the project actually is today, but here are some quick notes on a day with Rails:
### some notes on getting started
The main thing I learned about setting up a Rails project is that
1. it uses sqlite by default, you have to tell it to use Postgres
2. there are a ton of things that Rails includes by default that you can disable.
I installed and `rm -rf`d Rails maybe 7 times before I was satisfied with it and ended up with this incantation:
```
rails new . -d postgresql --skip-sprockets --skip-javascript`
```
Basically because I definitely wanted to use Postgres and not sqlite, and skipping sprockets and javascript seemed to make installing Rails faster, and I figured I could install them later if I decided I wanted them.
### the official Rails guide is really good
I used 2 main resources for creating my starter Rails app:
* DHHs original Rails talk from 2005 <https://www.youtube.com/watch?v=Gzj723LkRJY> (which I didnt watch this time, but I watched the last time I spent a day with Rails, and I found it pretty inspiring and helpful)
* The official Rails “getting started” guide, which seems pretty short and clear <https://guides.rubyonrails.org/v5.0/getting_started.html>
### a mysterious error message: `undefined method 'user'`
I love bugs, so heres a weird Rails error I ran into today! I had some code that looked like this:
```
@user = User.new(user_params)
@user.save
```
Pretty simple, right? But when that code ran, I got this baffling error message:
```
undefined method `user' for #<User:0x00007fb6f4012ab8> Did you mean? super
```
I was EXTREMELY confused about what was going on here because I hadnt _called_ a method called `user`. Id called `.save`. What???? I stayed confused and frustrated about this for maybe 20 minutes, and then finally I looked at my `User` model and found this code:
```
class User < ApplicationRecord
has_secure_password
validates :user, presence: true, uniqueness: true
end
```
`validates :user...` was _supposed_ to be some Rails magic validating that every `User` had a `username`, and that usernames had to be unique. But Id made a typo, and Id written `user` and not `username`. I fixed this and then everything worked! hooray!
I still dont understand how I was supposed to debug this though: the stack trace told me the problem was with the `@user.save` line, and never mentioned that `validates :user` thing at all. I feel like there must be a way to debug this but I dont know what it is.
The whole point of me playing with Rails is to see how the Rails magic plays out in practice so this was a fun bug to hit early on.
### a simple user management system
I decided I wanted users in my toy app. Some Googling showed me that theres an extremely popular gem called [devise][1] that handles users. I found the README a little overwhelming and I knew that I wanted a very minimal user management system in my toy app, so instead I followed this guide called [Authentication from Scratch with Rails 5.2][2] which seems to be working out so far. Rails seems to already have a bunch of built in stuff for managing users I was really surprised by how short that guide was and how little code I needed to write.
I learned while implementing users that Rails has a built in magical session management system (see [How Rails Sessions Work][3]. By default all the session data seems to be stored in a cookie on the users computer, though I guess you can also store the session data in a database if it gets too big for a cookie.
Its definitely kind of strange to already have a session management system and cookies and users without quite knowing whats going on exactly, but its also kind of fun! Well see how it goes.
### tomorrow: more rails!
Maybe tomorrow I can actually make some progress on implementing my fun rails app idea!
--------------------------------------------------------------------------------
via: https://jvns.ca/blog/2020/11/09/day-1--a-little-rails-/
作者:[Julia Evans][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://jvns.ca/
[b]: https://github.com/lujun9972
[1]: https://github.com/heartcombo/devise
[2]: https://medium.com/@wintermeyer/authentication-from-scratch-with-rails-5-2-92d8676f6836
[3]: https://www.justinweiss.com/articles/how-rails-sessions-work/

View File

@ -0,0 +1,95 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: (wxy)
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Day 1: a confusing Rails error message)
[#]: via: (https://jvns.ca/blog/2020/11/09/day-1--a-little-rails-/)
[#]: author: (Julia Evans https://jvns.ca/)
Rails 之旅第 1 天:一个令人困惑的 Rails 错误信息
======
今天,我开始了一个 Recurse Center 的批次学习!我认识了一些人,并开始了一个小小的有趣的 Rails 项目。我想我今天不会谈太多关于这个项目的实际内容,但这里有一些关于 Rails 一天的快速笔记。
### 一些关于开始的笔记
在建立 Rails 项目的过程中,我学到的主要是:
1. 它默认使用 sqlite你必须告诉它使用 Postgres。
2. Rails 默认包含了大量的东西,你可以禁用。
我安装并 `rm -rf` Rails 大概 7 次后才满意,最后用了这个咒语:
```
rails new . -d postgresql --skip-sprockets --skip-javascript
```
主要是因为我想用 Postgres 而不是 sqlite而且跳过 sprockets 和 javascript 似乎能让安装 Rails 的速度更快,而且我想如果我决定要它们的话,我可以在以后再安装。
### 官方的 Rails 指南真的很不错
我在创建我的 Rails 入门应用时主要参考了 2 个资源:
* DHH 在 2005 年的 Rails 原版演讲 <https://www.youtube.com/watch?v=Gzj723LkRJY>(这次我没有看,但上次我花了一天时间学习 Rails 时看了,我发现它很有启发和帮助)。
* 官方的 Rails 入门指南,似乎非常简短明了 <https://guides.rubyonrails.org/v5.0/getting_started.html>
### 一个神秘的错误信息:`undefined method 'user'`
我喜欢 bug所以今天我遇到了一个奇怪的 Rails 错误! 我有一些看起来像这样的代码:
```
@user = User.new(user_params)
@user.save
```
很简单吧?但当这段代码运行时,我得到了这个令人费解的错误信息:
```
undefined method `user' for #<User:0x00007fb6f4012ab8> Did you mean? super
```
我对这里发生的事情感到**超级**困惑,因为我没有调用一个叫做 `user` 的方法。我调用的是 `.save`。什么嘛?!我对此感到困惑和沮丧,大概呆了 20 分钟,最后我看了看我的 `User` 模型,发现了这段代码:
```
class User < ApplicationRecord
has_secure_password
validates :user, presence: true, uniqueness: true
end
```
`validates :user...` *应该*是一些 Rails 魔法,验证每个 `User` 都有一个 `username`,而且用户名必须是唯一的。但我犯了一个错,我写的是 `user` 而不是 `username`。我把这个问题解决了,然后一切都正常了!万岁。
我仍然不明白我应该如何调试这个问题:堆栈跟踪告诉我问题出在 `@user.save` 行,根本没有提到 `validates :user` 的事情。我觉得一定有办法调试这个问题,但我不知道是什么办法。
我学 Rails 的目的就是想看看 Rails 的魔力在实践中是如何发挥的,所以这是个很有意思的 bug早早的就掉坑里了。
### 一个简单的用户管理系统
我决定在我的玩具应用中加入用户。我在网上搜索了一下,发现有一个非常流行的叫做 [devise][1] 的工具可以处理用户。我发现它的 `README` 有点让人不知所措,而且我知道想要在我的玩具应用中建立一个非常简陋的用户管理系统,所以我遵循了这个名为《[Rails 5.2 中从零开始进行用户验证][2]》的指南到目前为止这个指南似乎还不错。Rails 似乎已经有了一大堆管理用户的内置东西,我真的很惊讶于这本指南的短小和我需要写的代码之少。
我在实现用户功能的时候了解到Rails 有一个内置的神奇的会话管理系统(参见 [Rails 会话如何工作][3]。默认情况下,所有的会话数据似乎都存储在用户电脑上的 cookie 中,不过我想如果 cookie 太大了,你也可以把会话数据存储在数据库中。
已经有了会话管理系统,有了 cookie 和用户,却不太清楚到底发生了什么,这肯定是有点奇怪的,但也是挺好玩的!我们会看看情况如何。我们将拭目以待。
### 明天:更多的 Rails!
也许明天我可以在实现我的有趣的 rails 应用的想法上取得一些进展!
--------------------------------------------------------------------------------
via: https://jvns.ca/blog/2020/11/09/day-1--a-little-rails-/
作者:[Julia Evans][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://jvns.ca/
[b]: https://github.com/lujun9972
[1]: https://github.com/heartcombo/devise
[2]: https://medium.com/@wintermeyer/authentication-from-scratch-with-rails-5-2-92d8676f6836
[3]: https://www.justinweiss.com/articles/how-rails-sessions-work/