Rails - 如何刷新页面而不重新加载它? (更新) [英] Rails - How do I refresh a page without reloading it? (updated)
问题描述
这个问题是关于我在问我先前的问题后遇到的问题 Rails - 如何刷新页面而不重新加载?
已经搜索,但找不到解决方案。
我建立了一个函数,每次人们去那个页面时都会发出一个随机记录,但如果用户刷新页面,随机记录会再次改变。 p>
因此我已尝试使用Cookie
#posts_controller.rb
def new
@random = cookies [:stuff] || = Stuff.random
end
并在我的 posts / new
视图中调用 @ random.content
只有 Stuff
模型的内容,当我第一次去页面是好的,但当我刷新页面,它给我这个错误
#< Stuff:0x0000010331ac00>的未定义方法'content':String
有办法解决这个问题吗?
谢谢!
----更新----
<%= show_random(@random)['content']%>
的 code>内部服务器错误预期为param'post'
使用
$ b
<%= f.hidden_field:stuff_id,:value => show_random(@random)[:id]%>
stuff / new.html.erb b
#app / views / stuff / new.html.erb
<%= show_random(@random)[:content] %>
< div>
<%= f.hidden_field:stuff_id,:value => show_random(@random)[:id]%>< br>
< / div>
但是当创建帖子而不满足验证时, / p>
没有将Stuff隐式转换为字符串
提取的源代码(第2行):
1
2<%= show_random(@random)['title']%>
我认为这与我在 Posts_controller.erb
请在下面查看我的 Posts_controller.erb
def create
@post = current_user.posts.build(post_params)
如果@ post.save
flash [:success] =已成功创建!
else
@random = Stuff.where(id:params [:post] [:stuff_id])。first
render'new'
end
end
所以一个新的Stuff实例被分配给两个cookie [:stuff]和@random,所以在@random的内容方法调用会很好。但是当你将一个对象存储到cookies [:stuff]中时,这个值会被rails自动转换为一个字符串。
第二次访问页面时, cookie [:stuff]不为空,并分配给@random变量。但是如前所述,cookie中的内容是一个字符串,因此调用字符串上的内容方法不能工作。
This question is about problems I ran into after asking my previous question Rails - How do I refresh a page without reloading it?
Have been googling but cannot find a solution.
I'm building a function that gives out a random record every time people go to that page but the random record will change again if the user refresh the page.
Therefore I have tried using cookies
#posts_controller.rb
def new
@random = cookies[:stuff] ||= Stuff.random
end
and call @random.content
in my posts/new
view since I want only the content of the Stuff
model, when I go to the page for the first time is fine but when I refresh the page, it gives me this error
undefined method 'content' for "#<Stuff:0x0000010331ac00>":String
Is there a way to resolve this?
Thanks!
----Update----
I have fixed the content
missing problem using <%= show_random(@random)['content'] %>
and the Internal Server Error expected Hash (got Array) for param 'post'
using
<%= f.hidden_field :stuff_id , :value => show_random(@random)[:id] %>
stuff/new.html.erb
# app/views/stuff/new.html.erb
<%= show_random(@random)[:content] %>
<div>
<%= f.hidden_field :stuff_id , :value => show_random(@random)[:id] %><br>
</div>
But when creating the Post without meeting the validation, it gives me
no implicit conversion of Stuff into String
Extracted source (around line #2):
1
2 <%= show_random(@random)['title'] %>
I think it has something to do with my create action in my Posts_controller.erb
Please have a look below of my Posts_controller.erb
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post created successfully!"
else
@random = Stuff.where(id: params[:post][:stuff_id]).first
render 'new'
end
end
The first time , as cookies[:stuff] is null, so a new Stuff instance is assigned to both cookies[:stuff] and @random, so the content method call on @random will be fine. But as you store an object into the cookies[:stuff], the value will be converted into a string by rails automatically.
The second time, you visit the page, the cookies[:stuff] is not empty, and is assigned to the @random variable. But as previous saying, the content inside the cookies is a string, so calling content method on a string can not work.
这篇关于Rails - 如何刷新页面而不重新加载它? (更新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!