如何在Rails的Simple Form表单中设置可修改的默认下拉选项
Got it, let's walk through building this feature step by step. The goal is to have a dropdown that remembers the user's last selection as the default, while still letting them change it anytime. Here's what you need to do:
1. Add Necessary Database Columns
First, we need two pieces of data stored in the database:
- The neighborhood selected for each order (add a column to your
orderstable) - The user's default neighborhood preference (add a column to your
userstable)
Run these commands in your terminal to generate and run the migrations:
# Add neighborhood field to orders rails generate migration AddNeighborhoodToOrders neighborhood:string # Add default_neighborhood field to users rails generate migration AddDefaultNeighborhoodToUsers default_neighborhood:string # Execute the migrations rails db:migrate
2. Update Controller Strong Parameters
Make sure your OrdersController permits the neighborhood parameter when creating an order. In app/controllers/orders_controller.rb:
private def order_params params.require(:order).permit(:neighborhood, # include other order attributes here) end
3. Build the Simple Form Dropdown
In your order form (likely app/views/orders/_form.html.erb), use Simple Form's input method to create the dropdown. We'll set the default value to the user's saved preference (if it exists), or fall back to your first option:
<%= simple_form_for @order do |f| %> <!-- Your other order form fields go here --> <%= f.input :neighborhood, collection: ["Marchmont", "New Town", "Baberton"], selected: current_user&.default_neighborhood || "Marchmont", label: "City Area" %> <%= f.button :submit %> <% end %> - The `current_user&.default_neighborhood` uses the safe navigation operator to avoid errors if the user isn't logged in. - We fall back to `"Marchmont"` as the initial default for first-time users or guests.
4. Save the User's Default Selection
After the user submits the order, update their default neighborhood to the one they just selected. In your OrdersController#create action:
def create @order = current_user.orders.build(order_params) # adjust this line if your order creation logic differs if @order.save # Update the user's default neighborhood if they're logged in current_user.update(default_neighborhood: @order.neighborhood) if current_user.present? redirect_to @order, notice: 'Order was successfully created.' else render :new end end
This ensures every order submission updates the user's default preference to their latest selection.
5. Allow Users to Modify Default Directly (Optional)
If you want users to change their default without submitting an order, add a settings page. For example, in app/views/users/edit.html.erb:
<%= simple_form_for current_user do |f| %> <%= f.input :default_neighborhood, collection: ["Marchmont", "New Town", "Baberton"], label: "Default City Area" %> <%= f.button :submit, "Update Default" %> <% end %>
And permit the parameter in your UsersController:
private def user_params params.require(:user).permit(:default_neighborhood, # include other user attributes here) end
That's all! Now your dropdown will auto-select the user's last chosen neighborhood by default, but they can still pick a different option whenever they want.
内容的提问来源于stack exchange,提问作者Steve

