我以前从未遇到过这个问题。我发现了这个错误。
No route matches [GET] "/recipes/1/like"
这是我的路线.rb:
Rails.application.routes.draw do
root 'pages#home'
get '/home', to: "pages#home"
resources :recipes do
member do
post 'like'
end
end
end
这是我的食谱控制器:
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 %>     
<%= 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 route请求,因为我需要web应用程序的这个特定部分的post请求,所以我从未在路由中创建过这个请求。
耙路:
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
我定义了操作,创建了类似的模型,将路由嵌套在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
如果有助于在上下文中看到它,请随意查看回购协议。这里有一个视图链接:https://github.com/SplitTime/OpenSplitTime/blob/master/app/views/events/splits.html.erb