Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 根据单选按钮选择增加字段值 ._Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 根据单选按钮选择增加字段值 .

Ruby on rails 根据单选按钮选择增加字段值 .,ruby-on-rails,ruby,Ruby On Rails,Ruby,这是我在视图中实现调查的全部代码。如果我把计数作为单选按钮中的第三个参数,我就不能为每个问题选择答案。我只能为整个调查选择一个答案。您可以使用虚拟属性进行此操作 attr_accessor :count_modification before_save :modify_count def modify_count() self.count += @count_modification if @count_modification end 鉴于: <%= radio_button

这是我在视图中实现调查的全部代码。如果我把计数作为单选按钮中的第三个参数,我就不能为每个问题选择答案。我只能为整个调查选择一个答案。

您可以使用虚拟属性进行此操作

attr_accessor :count_modification
before_save :modify_count

def modify_count()
    self.count += @count_modification if @count_modification
end
鉴于:

<%= radio_button("my_model", "count_modification", "1") %> Add one vote
<%= radio_button("my_model", "count_modification", "2") %> Add 2 votes
<%= radio_button("my_model", "count_modification", "3") %> Add 3 votes
添加一票
加2票
加3票

这样,投票将不会在模型保存后更新。

如果我从loop中获取单选按钮id,我可以将其放置在其中,例如:每个周期的计数值将增加。修改我的答案以考虑到这一点:)无论我做了什么更改,数据库中的值都不会更新。