首页 > 解决方案 > 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

标签: ruby-on-railsattributescrud

解决方案


我想使用多个提交按钮我认为您需要使用按钮标签:

  button_tag 'Run', type: :submit, name: 'pressed_button', value: 'run'
  button_tag 'Next', type: :submit, name: 'pressed_button', value: 'next'

现在,在您的控制器上,您可以检测到哪个按钮被按下,param[:button_pressed]将是runnext或者我认为nil您可以根据它采取不同的行动。


推荐阅读