You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby on Rails新手求助:获取select_tag选中值并传递至模型

Hey there! I totally get how tricky it can be to wrap your head around passing values between views, controllers, and models when you're new to Rails—let's break this down step by step to get you sorted.

Step 1: Fix the select_tag in your view

First, you need to make sure your select_tag generates a form field that gets submitted correctly alongside your form_for fields. Let's assume your form is tied to a model (like @task) and you want to select a multiplier value to multiply with your number field.

If you want the multiplier to be an extra parameter (not stored directly in your model), your view could look like this:

<%= form_for @your_model do |f| %>
  <%= f.label :remind_value %>
  <%= f.number_field :remind_value, placeholder: "Enter a base number" %>

  <%= label_tag :multiplier %>
  <%= select_tag :multiplier, options_for_select([["2x", 2], ["3x", 3], ["5x", 5]]), prompt: "Choose a multiplier" %>

  <%= f.submit "Save" %>
<% end %>

If you have a multiplier column in your model's database table, use f.select instead to keep it tied to the model:

<%= f.select :multiplier, options_for_select([["2x", 2], ["3x", 3], ["5x", 5]]), prompt: "Choose a multiplier" %>
Step 2: Update your controller to handle the parameters

Next, you need to make sure your controller accepts the submitted values. Here's how to adjust your create action:

Case 1: Multiplier is an extra parameter

def create
  @your_model = YourModel.new(your_model_params)
  # Grab the multiplier from the root params hash
  multiplier = params[:multiplier].to_i
  # Calculate and assign the result to your model (e.g., a `total` field)
  @your_model.total = @your_model.remind_value * multiplier

  if @your_model.save
    redirect_to @your_model, notice: 'Entry saved successfully!'
  else
    render :new
  end
end

private
def your_model_params
  # Permit your model's core fields
  params.require(:your_model).permit(:remind_value)
end

Case 2: Multiplier is a model field

def create
  @your_model = YourModel.new(your_model_params)

  if @your_model.save
    redirect_to @your_model, notice: 'Entry saved successfully!'
  else
    render :new
  end
end

private
def your_model_params
  # Add `multiplier` to the permitted params
  params.require(:your_model).permit(:remind_value, :multiplier)
end
Step 3: Move calculation logic to the model (best practice)

Rails encourages keeping business logic in models, not controllers. Use a callback to auto-calculate the product before saving:

class YourModel < ApplicationRecord
  # If using a virtual multiplier (not stored in DB), add this line
  attr_accessor :multiplier

  before_save :calculate_total

  private
  def calculate_total
    # Only run if both values exist
    if remind_value.present? && multiplier.present?
      self.total = remind_value * multiplier.to_i
    end
  end

If multiplier is a database column, just remove the attr_accessor line—Rails will handle it automatically.

Quick Troubleshooting Tips
  • Check your log/development.log to see exactly what params are being submitted. This will confirm if your select_tag value is coming through.
  • If you don't have a field to store the calculated total, generate a migration to add it:
    rails generate migration AddTotalToYourModels total:integer
    rails db:migrate
    
  • If using select_tag instead of f.select, remember the multiplier lives in params[:multiplier], not params[:your_model][:multiplier].

内容的提问来源于stack exchange,提问作者Ale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:11:45