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.
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" %>
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
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.
- Check your
log/development.logto see exactly what params are being submitted. This will confirm if yourselect_tagvalue 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_taginstead off.select, remember the multiplier lives inparams[:multiplier], notparams[:your_model][:multiplier].
内容的提问来源于stack exchange,提问作者Ale

