Rails表单未提交

时间:2018-12-14 17:30:51

标签: ruby-on-rails

我正在尝试制作一个Rails表单,不幸的是它不会提交。我相信这是由于尝试显示“ index.html.erb”时出现的错误:

No route matches {:action=>"show", :controller=>"recipes", :method=>"get"}, missing required keys: [:id]

查看:

<!DOCTYPE html>
<html>
<head>
    <title>Recipe List</title>
</head>
<body>
    <h1>Recipes</h1>
    <%= form_tag recipe_path :method => 'get' do %>
    <%= search_field_tag :search, params[:search], placeholder: "Search recipes" %>
    <%= submit_tag "Search" %>
    <% end %>

<% if @recipe.present? %>
  <%= render @recipe %>
<% else %>
  <p>There are no recipes containing the term(s) <%= params[:search] %>.</p>
<% end %>

    <% @recipes.each do |s| %>
    <span class="name"><%= s.name %></span>
    <p><%= s.scam %></p>
    <% end %>
</body>
</html>

控制器:

class RecipeController < ApplicationController

  def index
    recipe_path = '/recipe'
  @recipe = Recipe.all
  if params[:search]
    @recipe = Recipe.search(params[:search]).order("created_at DESC")
  else
    @recipe = Recipe.all.order('created_at DESC')
  end
 end

end

型号:

class Recipe < ApplicationRecord

    def self.search(search)
            where("name LIKE", "%#{search}%")
    end
end

路由文件:

Rails.application.routes.draw do

  resources :recipes
  get 'recipe' => 'recipe#index'
   # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

1 个答案:

答案 0 :(得分:0)

欢迎使用StackOverflow。

我看到了几个问题。但是,阻止您的文件位于routes文件中。 resources :recipes为您声明了几条路线,包括/recipes/:id,它被称为recipes_path。这就是您在表单中使用的路线。因此,您的表单需要:id参数。因此,您看到的错误。