ruby-on-rails - 在rails中自动为用户创建记录

标签 ruby-on-rails ruby

我正在为我的 Rails 应用程序开发状态功能。我已经把一切都设置好了并且工作得很好。对于界面,我使用 best_in_place gem 来允许用户就地编辑状态。代码如下。

<% if @scoreboard.status.content.present? %>
  <div id="statuscontent">
    <%= render 'statusedit' %>
  </div>
<% else %>
  <%= render 'statusupdate' %>
<% end %>

statusedit 部分包含 best_in_place 代码,statusupdate 部分包含用于创建状态的标准形式。下面给出了两个部分的代码。

状态编辑

<%= best_in_place [@scoreboard, @status], :content, place_holder: "Enter a status", as: "textarea" %>
<%= link_to "delete", [@scoreboard, @status], method: :delete, data: {confirm: "Are you sure"} %>

状态更新

<%= form_for [@scoreboard, @status] do |f| %>  
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.text_area :content, class: "form-control", id:"status"  %>
  <%= f.submit "Post", class: "btn btn-primary", id: "status-button" %>  
<% end %>

上面的代码工作得很好,但是,我很难设置我想要的界面。问题在于,只有之前已创建过状态,best_in_place 编辑才有效。因此,我必须使用表单创建状态,然后才能使用就地编辑。但是,我希望用户可以使用就地编辑界面,而无需先物理创建状态。有没有办法为每个用户创建默认状态?或者一个 before_update 函数,可以在用户尝试更新之前创建一个状态。

我尝试使用 Ajax 创建状态来实现该界面,但它没有按预期工作。另外,我觉得这个问题必须有一个更简单的解决方案。我尝试在数据库中设置 :default => "please upload status" 但这实际上并没有创建默认记录。它的作用就像一个占位符,但您仍然需要单击“发布”才能创建记录。有没有办法以某种方式自动为每个用户创建默认状态或在数据库中设置一个值。我读过一些关于此的堆栈溢出帖子,但没有任何内容真正指向正确的方向。在 Rails 中必须有一种更简单的方法来做到这一点。任何文档或建议都会有很大帮助。一如既往,我们非常感谢任何帮助!谢谢 我已经添加了相关的迁移和模型文件。

status model
    class Status < ActiveRecord::Base
      belongs_to :scoreboard
    end

scoreboard model
    class Scoreboard < ActiveRecord::Base
      has_one :status
    end

Status migration file

class CreateStatuses < ActiveRecord::Migration
  def change
    create_table :statuses do |t|
      t.text :content
      t.references :scoreboard, index: true

      t.timestamps null: false
    end
    add_foreign_key :statuses, :scoreboards
  end
end

最佳答案

回答您的问题。

Is there a way to create a default status for every user ?

是的

您可以在 change_column 之后在数据库中创建一条默认记录.

def up
  change_column :users, :admin, :boolean, default: false   # I'm assuming you are saving default as false in users table but you could change accordingly all thing. 
end

before_update function that maybe creates a status before the user tries to update.

可以使用 after_save 回调代替 before_update

As mentioned in official Document

after_save runs both on create and update, but always after the more specific callbacks after_create and after_update, no matter the order in which the macro calls were executed.

最后

There must be a simpler way of doing this in rails

使用 ActiveRecord 迁移。

关于ruby-on-rails - 在rails中自动为用户创建记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34460085/

相关文章:

ruby - 使用 2 次搜索进行 Mechanize 抓取?

ruby - 如何将数组中的多个对象铲到一个对象中?

ruby-on-rails - ruby 和 rails 中的资源路由

ruby-on-rails - Ruby(带 Rails)将一串时间转换成秒?

ruby-on-rails - 在 Capistrano 重新索引太阳黑子 Solr

ruby-on-rails - 根据 child 的创建日期对对象进行排序

ruby - 在 ruby​​ 循环中运行几个 'exec'

ruby-on-rails - Rspec 测试以 'rspec' 失败,但以 'rspec path_of_the_file' 失败

ruby-on-rails - 将 Cappuccino 与 Ruby on Rails 集成

ruby-on-rails - 限制 Nokogiri 中 XPath 的搜索范围