【问题标题】:Rails - How do I refresh a page without reloading it? (updated)Rails - 如何在不重新加载的情况下刷新页面? (更新)
【发布时间】:2014-04-12 07:00:48
【问题描述】:

这个问题是关于我在问了上一个问题Rails - How do I refresh a page without reloading it?之后遇到的问题

一直在谷歌搜索,但找不到解决方案。

我正在构建一个函数,每次人们访问该页面时都会发出随机记录,但如果用户刷新页面,随机记录将再次更改。

因此我尝试使用 cookie

#posts_controller.rb    

def new
  @random = cookies[:stuff] ||= Stuff.random
end

并在我的posts/new 视图中调用@random.content,因为我只想要Stuff 模型的内容,当我第一次进入页面时很好,但是当我刷新页面时,它给了我这个错误

undefined method 'content' for "#<Stuff:0x0000010331ac00>":String

有没有办法解决这个问题?

谢谢!

----更新----

我已经使用 &lt;%= show_random(@random)['content'] %&gt;Internal Server Error expected Hash (got Array) for param 'post'using 修复了 content 缺失问题

<%= 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> 

但是在没有通过验证的情况下创建帖子时,它给了我

no implicit conversion of Stuff into String

Extracted source (around line #2):

1
2  <%= show_random(@random)['title'] %>

我认为这与我在 Posts_controller.erb 中的创建操作有关

请看下面我的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

【问题讨论】:

    标签: ruby-on-rails cookies ruby-on-rails-4


    【解决方案1】:

    第一次,因为 cookies[:stuff] 为 null,所以新的 Stuff 实例被分配给 cookies[:stuff] 和 @random,所以 @random 上的 content 方法调用就可以了。但是当你将一个对象存储到 cookies[:stuff] 中时,这个值会被 rails 自动转换成一个字符串。

    第二次访问页面时,cookies[:stuff] 不为空,并赋值给@random 变量。但是如前所述,cookies里面的内容是字符串,所以在字符串上调用content方法是行不通的。

    【讨论】:

    • 感谢您的回答,有没有办法在视图中再次将存储的cookie调用为model,以便我可以调用方法content?当用户刷新页面时,内容保持不变,直到 cookie 过期。
    • 您可以只将内容存储在 cookie 中而不是 Stuff 实例中。否则就得自己来回序列化了。
    【解决方案2】:

    使您在Stuff 上定义的.random 方法返回一个序列化 记录,然后您可以在视图中对其进行反序列化(可能使用辅助方法)以使其成为哈希:

    # app/models/stuff.rb
    def self.random
      random_record_magic.to_json
    end
    end
    
    # app/views/stuff/index.html.erb
    <%= show_random(@random)['content'] %>
    
    # app/helpers/stuff_helper.rb
    def show_random(random)
      JSON.parse(random)
    end
    

    【讨论】:

    • 嗨阿吉斯。感谢您的回答,由于某种原因,我的观点是空白的。我已经打电话给&lt;%= show_random(@random)[:content] %&gt;,但content 没有显示。
    • 我在rails控制台中测试过,show_random(@random)[:content]返回nil这是怎么回事? show_random(@random) 返回{"id"=&gt;2, "content"=&gt;"SUP!", "created_at"=&gt;"2014-03-09T03:16:21.501Z", "updated_at"=&gt;"2014-03-09T03:16:21.501Z"}
    • OK 更新了我的代码。应该是show_random(@random)['content']
    • 感谢更新,您知道导致Internal Server Error expected Hash (got Array) for param 'post' 错误的原因吗?当我尝试创建帖子时会发生这种情况。 &lt;%= show_random(@random)[:content] %&gt; 在我的帖子中/new.html.erb
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 2020-10-09
    相关资源
    最近更新 更多