ruby-on-rails - Ruby on rails next 按钮链接到下一个位置编号
问题描述
我有 Rails 应用程序,我想添加下一个按钮我有单位,里面有位置
我希望当用户按下下一个按钮时链接到下一个位置编号
create_table "units", force: :cascade do |t|
t.string "title"
t.string "unit_type"
t.string "body"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "section_id"
t.integer "position"
t.index ["section_id"], name: "index_units_on_section_id"
end
这是我的单元秀
<%= form_for [@unit, @attempt] do |f| %>
<%= f.text_area :body,id: "editor" %>
<%= f.hidden_field :log,id: "ans"%>
<%= f.submit 'Run', type: "button" ,id: "equal"%>
<%= f.submit "Next"%>
例如,我现在在位置为 1 的单元 1 中,当我按下下一步时,我想带我到位置为 2 的单元 # 等等
这是我的单位模型
class Unit < ApplicationRecord
belongs_to :section
has_many :attempts, dependent: :destroy
def attempt_for(user)
attempts.find_by_user_id(user)
end
end
尝试模型
class Attempt < ApplicationRecord
belongs_to :user
belongs_to :unit
end
内部尝试控制器
def create
@attempt = @unit.attempts.new(attempt_params)
@attempt.user = current_user
if @attempt.save
array = Unit.where(position: 1).pluck(:id)
index_a = array[1]
redirect_to unit_path(index_a), notice: "good jpb!"
else
redirect_to unit_path(), alert: "Try again!"
end
end
解决方案
我想使用多个提交按钮我认为您需要使用按钮标签:
button_tag 'Run', type: :submit, name: 'pressed_button', value: 'run'
button_tag 'Next', type: :submit, name: 'pressed_button', value: 'next'
现在,在您的控制器上,您可以检测到哪个按钮被按下,param[:button_pressed]
将是run
,next
或者我认为nil
您可以根据它采取不同的行动。
推荐阅读
- c++ - 引用命名空间会留下随机值吗?
- excel - 使用 VBA 在 excel 中创建每月选项卡
- javascript - 如何将 javascript 代码从导入添加到 iframe?
- python - 用 Python 填写动态在线表格
- uwp - VSTS - UWP - 多语言应用工具包 - 构建任务和生成的语言错误
- java - 从主机到 HDFS 的调用因连接异常 java.net.ConnectException 失败:连接被拒绝
- javascript - 如何将 JavaScript XMPP 客户端(JSXC)配置到网站?
- serialization - 不能腌制弱引用对象
- python - 总和是做什么的?
- string - 查找一个字符串并将其整行复制到一个新文件中