加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ruby?? on rails id不能保存

发布时间:2020-12-17 02:57:52 所属栏目:百科 来源:网络整理
导读:我知道这违反了 ruby on rails约定,但是我对这个表的id不需要自动递增,而是我通过逻辑设置它.但是,它不会保存到数据库中.保存的所有内容都是为id保存为null. def self.up create_table :probes,:id = false do |t| t.string :id t.string :name t.integer :u
我知道这违反了 ruby on rails约定,但是我对这个表的id不需要自动递增,而是我通过逻辑设置它.但是,它不会保存到数据库中.保存的所有内容都是为id保存为null.

def self.up
 create_table :probes,:id => false do |t|
  t.string :id
  t.string :name
  t.integer :user_id
  t.boolean :online
  t.timestamps
 end
end

<% form_for @probe do |f| %>
<%= f.error_messages %>
 <p>
  <%= f.label "Site name" %><br />
  <%= f.text_field :name %>
 </p>
     <p style="margin-left: 10%">
  <%= f.label "Probe Key" %><br />
  <%= f.text_field :id,:value => @token %>
 </p>
 <p style="margin-left: 20%">
  <%= link_to "Back to List",probes_path %>
  <%= f.submit "Submit",:style => "margin-left: 75px;" %></p>
 <% end %>

这甚至可能吗?或者除了new.html.erb文件之外还有哪些地方我应该更改/检查?

解决方法

对于质量分配,无法访问:id字段.你需要手动设置它

@probe.id = params[:probe][:id]

在您的控制器代码中.

(如果你在你的attr_accessible列表中添加:id也可以工作,一般来说你应该为每个从表单参数直接批量分配的模型设置attr_accessible,但没有测试我不确定它是否可行,您可能仍需要手动设置:id)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读