Rails - 如何设置 id 作为令牌(预测试版邀请应用程序,如 usehipster)
我正在尝试编写一个“病毒式”预测试版邀请应用程序,正如人们可以在 usehipster.com 或 fork 上看到的那样。 ly。
基本上,未来的测试人员:
1.) 输入他的电子邮件
2.) 被重定向到一个视图(即将到来的页面)
3.) 收到如下链接:“http://localhost:3000/?referred_to=测试者的邀请 ID”显示在视图中。
4.) 并收到一封带有相同链接的电子邮件。
如果我理解得很好,“测试人员的邀请 ID”充当令牌,以便跟踪邀请来自哪些测试人员。
我的问题:
1.)如何生成链接中的 id?我无法使用 before_create 因为测试人员注册时尚未设置邀请 ID。
我试过这个:
邀请控制器中
def coming_soon
@invitation = Invitation.last
end
在views/invitations/coming_soon.html.erb的
...
Copy and paste the following link to share wherever you want!</p>
<%= text_field_tag 'code', root_url + "?reffered_by=" + @invitation.id.to_s %>
你认为他们这样做吗?
2.) 为什么链接里有一个问号? (或者像 ?reffered_by= 为什么不只是 root_url/@invitation.id.to_s )这与路由有关?它是一个get方法吗?
感谢您的帮助!
I am trying to write a "viral" pre-beta invitation application as one can see on usehipster.com or fork.ly.
Basically, the future tester:
1.) enters his email
2.) is redirected to a view (a coming_soon page)
3.) receive a link like this one : "http://localhost:3000/?referred_to=the tester's invitation id" displayed in the view.
4.) and receive an email with the same link.
If I understand it well, the "tester's invitation id" acts as a token in order to track from which testers the invitation come from.
My questions:
1.) How can I generate the id in the link? I cannot use before_create cause the invitation id is not already set up when a tester registered.
I tried this:
in the invitation controller
def coming_soon
@invitation = Invitation.last
end
in views/invitations/coming_soon.html.erb
...
Copy and paste the following link to share wherever you want!</p>
<%= text_field_tag 'code', root_url + "?reffered_by=" + @invitation.id.to_s %>
Do you think they do like this?
2.) Why there is a question mark in the link? (or something like ?reffered_by= why not just root_url/@invitation.id.to_s) This is something related to the routes? is it a get method?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我来回答问题2
:? in url 是一种传递参数的方式。使用 GET 方法的表单使用这种附加到 url 来传递参数。 URL ?reffered_by=somevalue 传递带有值 somevalue 的参数refered_by。现在,如果在我们的控制器中,我们需要这个值,我们可以调用 params[:referred_by] 我们将得到“somevalue”的值。如果我们想传递多个参数,我们可以使用 & 来传递。例如,
现在,我们可以在控制器中以 params[:referred_by] 和 params[:referred_time_stamp] 的形式访问这些参数。
如果在routes.rb中没有定义路由或资源,则无法使用 id 等参数传递
“#{root_url}/#{@invitation.id}”
。例如,当您在routes.rb中定义时,它会理解最后一个斜杠后面的部分是参数id的值,我们将在控制器中将其作为
params[:id]
获取。I will answer question 2:
? in url is a way to pass parameters. A form using GET method uses this sort of appending to url to pass parameters. URL ?reffered_by=somevalue passes the parameter referred_by with value somevalue. Now if in our controller, we want this value, we can call
params[:referred_by]
and we will get the value as "somevalue". If we want to pass more than one parameter, we can pass it using &. For example,Now, we can access these parameters in controller as params[:referred_by] and params[:referred_time_stamp].
If you have no routes or resources defined in routes.rb, you cannot pass parameters like id using
"#{root_url}/#{@invitation.id}"
. For example, when you definein routes.rb, then it understands the part coming after last slash is the value of parameter id and we will get it as
params[:id]
in the controller.