Why are database entries being automatically created when I visit the “new” page?

后端 未结 1 1437
野趣味
野趣味 2021-01-29 03:09

I\'m having trouble understanding why my app is automatically creating entries in the database when I visit the \"new\" page. The page is supposed to have a form that, when subm

相关标签:
1条回答
  • 2021-01-29 04:02

    In the controller, the logic to create a Recipe has been written in new method and just a recipe is just initialized in create method. But in the routes the create method has a POST call(form submission with data) and the new method is a GET.

    So all you need to do is just change the name of the 'create' method as 'new' and change the name of the 'new' method as 'create' in your controller.

    i.e)

    def create
        @recipe = Recipe.create(params[:recipe])
        if @recipe.save
            redirect_to recipe_new_path
        else
            reload_page
        end
      end
    
      def new
       @recipe = Recipe.new
      end
    

    This will work.

    0 讨论(0)
提交回复
热议问题