rails 4 populate dropdown values from database

后端 未结 3 718
清酒与你
清酒与你 2021-02-06 01:26

I have a dropdown in a rails form:

<%= f.select :lists, [[\"test1\", 1], [\"test2\", 0]] %>

This works fine but how can I make it dynamic

3条回答
  •  清酒与你
    2021-02-06 02:04

    Don't quite have enough reputation to respond to your question in the thread above @learner but there's a good chance that @overflow didn't have @list defined in his controller.

    To solve my case I put my equivalent of @list (in this case @restaurants) in my "def new" method since I was using it to help create new items with associated restaurants.

    # GET /specials/new
      def new
        @special = Special.new
        @restaurants = Restaurant.all // Look Here
      end
    

    Additionally, :all_val in the original response should be the parameter you want to pass in to the database. In my case it was :restaurant_id

提交回复
热议问题