我有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,我想当我按下next键时,把我带到位置为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
,你可以根据它采取不同的行动。