ruby-on-rails - 没有路由匹配 [GET] "/recipes/1/like"我想要一个 POST 请求

标签 ruby-on-rails ruby http post

我以前从未遇到过这个问题。我收到此错误。

No route matches [GET] "/recipes/1/like"

这是我的 routes.rb:

Rails.application.routes.draw do

  root 'pages#home'

  get '/home', to: "pages#home"

  resources :recipes do
    member do
      post 'like'
    end
  end
end

这是我的 recipes_controller:

  def like
    @recipe = Recipe.create(params[:id])
    Like.create(like: params[:like], chef: Chef.first, recipe: @recipe)
    #flash message
    flash[:success] = "Your selection was sucessful"
    redirect_to :back
  end

这是我的 html.erb 文件:

<%= render 'shared/page_title', title: @recipe.name.titleize  %>

<div class= "row">
  <div class="col-md-4 pull-right center">
    <%= gravator_for @recipe.chef, size: 200 %>
    <p>
      <h5>Brought to you by: <%= @recipe.chef.chefname.capitalize %></h5>
    </p>
  </div>
  <div class= "col-xs-8 col-md-8">
    <%= link_to "Edit this Recipe", edit_recipe_path(@recipe), class: "btn btn-success pull-right" %>
    <h3><%= @recipe.summary.capitalize %></h3>
    <div class="show_recipe">
      <%= image_tag(@recipe.picture.url, size: "300x200", class: "recipe-image") if @recipe.picture? %>
    </div>
    <div class ="well recipe-description">
      <p>
        <strong> Steps:</strong>
      </p>
        <%= simple_format(@recipe.description) %>
        <div class="pull-right">
          <%= link_to like_recipe_path(@recipe, like: true), method: :post do %>
            <i class="glyphicon glyphicon-thumbs-up"></i>
          <% end %> &nbsp&nbsp&nbsp&nbsp
          <%= link_to like_recipe_path(@recipe, like: false), :method => :post do %>
            <i class="glyphicon glyphicon-thumbs-down"></i>
          <% end %>
        </div>
    </div>
  </div>
</div>

<h5><%= link_to "Return to Recipes Listings", recipes_path, class: "btn btn-warning btn-small" %></h5>

我已经明确地将 HTTP POST 请求添加到我的 html.erb 文件中

%= link_to like_recipe_path(@recipe, like: true), method: :post do %>

但 rails 提示没有 GET 路由请求,我从未在我的路由中创建该请求,因为我需要对 Web 应用程序的这个特定部分进行 POST 请求。

rake 路线:

     Prefix Verb   URI Pattern                 Controller#Action
       root GET    /                           pages#home
       home GET    /home(.:format)             pages#home
like_recipe POST   /recipes/:id/like(.:format) recipes#like
    recipes GET    /recipes(.:format)          recipes#index
            POST   /recipes(.:format)          recipes#create
 new_recipe GET    /recipes/new(.:format)      recipes#new
edit_recipe GET    /recipes/:id/edit(.:format) recipes#edit
     recipe GET    /recipes/:id(.:format)      recipes#show
            PATCH  /recipes/:id(.:format)      recipes#update
            PUT    /recipes/:id(.:format)      recipes#update
            DELETE /recipes/:id(.:format)      recipes#destroy

老实说,我迷路了。似乎一切都在正确的地方。

rails 版本:

Rails 4.2.5

我已经定义了操作,创建了 like 模型,将路由嵌套在 recipes 下,并在 html.erb 页面中明确请求了一个 post HTTP 请求。

任何想法都会很棒!

干杯!

最佳答案

这是我的工作项目中具有类似安排的相关代码。

# /views/events/splits.html.erb:

<%= link_to "Add",
   associate_splits_event_path(id: @event.id, split_ids: [split.id]),
   :method => :put,
   :class => 'btn btn-xs btn-success' %>

# routes.rb

  resources :events do
    member { put :associate_splits }
  end

如果在上下文中查看它有帮助,请随意浏览存储库。这是 View 的链接:https://github.com/SplitTime/OpenSplitTime/blob/master/app/views/events/splits.html.erb

关于ruby-on-rails - 没有路由匹配 [GET] "/recipes/1/like"我想要一个 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40513600/

相关文章:

html - 如何修改和设置简单表单 f.input 标签的样式

ruby-on-rails - 是否有更好的 Ruby 或 Rails 习惯用法来检查嵌套哈希中是否存在值?

javascript - JwPlayer:找不到 HTML5 播放器

http - 如何使用 gorequest 发起 POST 请求

node.js - 慢速磁盘 I/O 是否会降低 Node.js 应用程序其余部分的性能?

ruby-on-rails - 如何解决 "LoadError: cannot load such file -- 2.5/ffi_c"Running Ruby on Rails with WSL

css - 在 Rails 中如何根据 URL 更改导入的样式

ruby - Ruby 中的 Project Euler #6(有什么区别?)

ruby-on-rails - rails : test single file with rake test without resetting database

http - 为什么当我输入 http URL 时 firefox 立即请求 https?