gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:47:05 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com