问题标签 [railstutorial.org]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
572 浏览

javascript - jQuery/Rails 3 button does not change state

I have spent a couple of days (like 4) trying to solve this issue. I followed the Hartl Rails 3 tutorial and in chapter 12 tried to convert the site from prototype to jQuery. I am not able to get the "follow"/"unfollow" button to update however.

I am able to issue jQuery commands from within Safari's Inspect Element Console, but if I put even the simplest jQuery command into the destroy.js.erb or create.js.erb files, nothing happens. The log is indicating that the appropriate relationships/destry.js.erb (or create.js.erb) file is rendering.

Here is the code that I have in the controller:

The users/_follow.html.haml is

The users/_unfollow.html.haml is

The users/_follow_form.html.haml is

The relationships/create.js.erb is

The relationships/destroy.js.erb is

However, in trying to diagnose this, I tried a very basic

which also does not work.

0 投票
1 回答
280 浏览

ruby-on-rails - 从需要 attr_accessors 的检索对象更新属性 - Rails 教程 - Michael Hartl's

在Michael Hartl 的教程的第 7 章中,有一个用户模型 < code here >,它的密码属性定义为 attr_accessor 和 attr_accessible,并带有一个存在验证器。

问题是:如果我检索现有用户并尝试更新其电子邮件,ruby 会抛出一个要求其密码的异常,并且每次我想更新任何其他属性时都被迫重新设置密码。例子:

我在密码的存在验证器旁边添加了“:on =>:create”,它似乎解决了这个问题。这是关于登录系统的正确解决方案吗?

任何完成过 Michael Harlt 教程的人都知道这是一个错误还是他的真实意图?

0 投票
2 回答
126 浏览

ruby-on-rails - Rails 教程:没有关系控制器

我正在关注 rails 控制器,并且对为什么它们不是控制器感到困惑。我不记得它要求我生成一个,但我应该吗?我很困惑?谢谢!

0 投票
2 回答
11072 浏览

ruby-on-rails - Rails 验证错误消息显示错误消息和数组

我正在阅读 Ruby-on-Rails3 教程第 8 章。

我创建了一个用户提交以注册网站的表单。

验证在模型中设置。他们已经过测试并且正在工作。

问题是当我显示验证错误消息时,执行以下操作:

我不仅得到了验证消息,而且还显示了实际的数组:

对于我的生活,我无法弄清楚为什么。有谁知道如何阻止它显示?

提前致谢。

这是 Rails3.0.7 Ruby 1.9.2 OSX10.6

0 投票
2 回答
33160 浏览

ruby - Rails——创造和创造!方法,RoR 3 教程

所以我知道 bang(感叹号)和非 bang 方法之间的区别通常是该方法是修改对象本身还是返回一个单独的修改后的对象,保持原始对象不变。

然后,在本书第 6 章中构建用户模型时,我遇到了一个User.create方法,该方法可以创建一个新模型并将其保存到数据库中。在 Michael Hartl 的Ruby on Rails 3 教程中,他写道,该User.create!方法“与该create方法一样工作......除了它在创建失败时引发ActiveRecord::Record-Invalid异常”。

我很困惑。该User.create!方法是不是遵循 Ruby “bang-convention”,还是我完全遗漏了什么?如果他遵循惯例,User.create!如果它是类方法,如何修改 self ?

0 投票
1 回答
3375 浏览

ruby-on-rails - RSPEC 错误:验证失败:电子邮件已被使用 Rails 教程

我在运行测试时遇到这些错误:

它似乎不喜欢这条线:

工厂代码

我想这就是你想要的。

用户控制器规范

0 投票
1 回答
416 浏览

ruby-on-rails - Rails 教程 - 按照显示为转义 html 的表单

与我的上一个问题类似:Issue with escaped html in .js.erb file

我正在尝试在 rails 教程中呈现以下表单,但我再次转义了 html。有没有类似的套路?

$("#<%= "follow_form#{@tag.id}" %>").html("<%=escape_javascript(render('tags/follow', :tag => @tag)) %>") ;

我明白了

当我只想获得不同的提交按钮时。

视图代码:

我继续调查。如果我添加这个:

$("#<%= "follow_form#{@tag.id}" %>").html("<%= escape_javascript(render('tags/follow', :tag => @tag)).html_safe %> ");

我得到了适当的表格来显示,但是:

div>按钮div>表单>

而不仅仅是

按钮

0 投票
1 回答
1041 浏览

ruby-on-rails - JS请求连续被调用两次

我正在研究 Ruby on Rails 教程。我有异步跟随按钮工作。有趣的是,当我单击按钮时,每个 javascript 调用都会连续调用两次。关于如何使它只发送一个请求的任何想法?这是我的日志

这是我的看法

还有我的控制器

0 投票
1 回答
2297 浏览

ruby-on-rails - RSPEC 测试失败,应用程序正常工作

我正在关注 Rails 教程,希望对 RSPEC 错误有所帮助。

错误:

*更新:这些错误并没有消失!*

用户控制器规格:

用户规格:

友好转发规范:

工厂:

0 投票
1 回答
486 浏览

ruby-on-rails-3 - railstutorial.org lesson 11: time_ago_in_words not posting time

working on lesson 11 in the tutorial and i ran db:populate to show 50 microposts for the first 6 users

but when looking on browser page it says "Posted ago" on all microposts

in micropost.html.erb file:

any ideas?

update 1:

trying to trial and error and when i change the ago. to agooooo. just to see if it changes on browser... it doesnt... i restarted autotest, spork and, rails s to see if it would show the Posted agoooooo. and nothing changes just holds "Posted ago."

update 2:

if you need to see my show.html.erb file

Update 3:

adding the help. before may work but im confused why when i change Posted or ago to different words they dont change at all on the browser... whats the best way to send a full refresh so server sees these changes... tried restarting rails server, spork and autotest

Update 4:

I somehow have 2 _micropost.html.erb files in different locations.... solving problem now will post again in moment

Update 5:

I had a micropost folder with _micropost.html.erb file inside my users folder... which i had open

the correct micropost folder inside app => views => microposts => microposts.html.erb was missing a = in the <%=

I had been editing the wrong file the whole time i believe

As for adding a helper. -- it didnt work but it lead me to the problem -- so i consider that a solution in my book!

thank you very much ardavis!