我的提交按钮将不再有效。与我的控制器有什么关系?

问题描述 投票:0回答:1

我的提交按钮不会将任何信息发布到数据库中,也不会在按下按钮后将其自身重定向回主页。需要帮助指出代码中的潜在错误

这是我必须提交课程的rails应用程序。我已经尝试更改提交按钮在html文件中的位置。更改错误检查和表单。但提交按钮和更新操作将无法启动

edit.html.erb

<div>
<h1 align="center">Edit Recipe</h1>

    <%= form_for :recipe, url: recipes_path, method: :patch do |f| %>
    <% if @recipe.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@recipe.errors.count, "error") %> prohibited
        this recipe from being saved:
      </h2>
      <ul>
        <% @recipe.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

<h2><strong><%=f.label :Title %></strong></h2>
<p>
    <%=f.text_field :title %>
</p>

<h2><%= f.label :Description %></h2>
   <p>
     <%= f.text_field :description %> 
  </p>
  </div>

  <div> 
<h2><%= f.label :Instruction %></h2>
  <p>
    <%= f.text_field :instruction %>
    </p>
   </div>

    <div> 
<h2> <%= f.label :Ingredients %></h2>
    <p>
    <%= f.text_field :ingredients %>
  </p>
  <p>
        <%= f.submit %>
    </p>
  </div>

    <% end %>
    <%= link_to 'Back', home_page_index_path %>

recipe_controller.rb

class RecipesController < ApplicationController
    def index
        @recipes = Recipe.all
    end
    def new
        @recipe = Recipe.new
    end
    def show
        @recipe = Recipe.find(params[:id])
    end
    def create 
        @recipe = Recipe.new(recipes_params)
        if @recipe.save
            redirect_to @recipe
        else
            render 'new'
        end
    end
    def edit
        @recipe = Recipe.find(params[:id])
    end
    def update
        @recipe = Recipe.find(params[:id])
        if @recipe.update(recipe_params)
            redirect_to @recipe
        else
            render 'edit'
        end
    end
    private
        def recipes_params
            params.require(:recipe).permit(:title, :instruction, :ingredients, :description)
        end
end

recipe.rb

class Recipe < ApplicationRecord
    validates :title, presence: true, length: { minimum: 5 }
end
ruby-on-rails ruby model-view-controller ruby-on-rails-5
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.