我有一个带有状态字段的Post表
t.string :state
我使用Classy Enum宝石通过State生成的类创建了我的3个状态
class State < ClassyEnum::Base
end
class State::New < State
end
class State::Published < State
end
class State::Hidden < State
end
我有一个具有更新方法的post_controller
我认为我有此表单来提交更改
<%= form_for @post, method: :patch do |f| %>
<%= f.hidden_field state: "published" %>
<%= f.submit "Share", class: "btn btn-primary"%>
<% end %>
为什么会出现此错误?
ActionView::Template::Error (undefined method `{:state=>"published"}' for #<Post:0x00007f973fe62ef8>)
答案 0 :(得分:0)
在帖子模型中添加state:string
rails g migration add_state_to_posts state
或
class Post < ActiveRecord::Base
include ClassyEnum::ActiveRecord
classy_enum_attr :state
end
参考=> https://github.com/beerlington/classy_enum#3-setup-the-active-record-model
答案 1 :(得分:0)
这是答案:
<%= link_to 'Publish', post_path, data: {method: :patch, remote: true, params: {state: "published"}.to_param}, class: "btn btn-primary" %>
如果有人可以帮助,我只需要form_for方式