我之前从未遇到过这个问题。我收到了这个错误。
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 %>     
<%= 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请求。
耙路线:
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
我已经定义了操作,创建了类似的模型,在配方下嵌套了路由,并在html.erb页面中显式请求了后HTTP请求。
任何想法都会很棒!
干杯!
答案 0 :(得分:1)
这是我工作项目中的相关代码,具有类似的安排。
Tanswer=float(raw_input("Enter The Tangent:"))
如果在上下文中看到它有帮助,请随意浏览回购。这是指向视图的链接:https://github.com/SplitTime/OpenSplitTime/blob/master/app/views/events/splits.html.erb
答案 1 :(得分:0)
尝试
<%= link_to "LIKE", like_recipe_path(@recipe) , method: :post %>
其他选择:
<%= link_to '<i class="glyphicon glyphicon-thumbs-up"></i>'.html_safe , like_recipe_path(@recipe , like: 'value' ) , method: :post %>
答案 2 :(得分:0)
虽然rails link_to
允许链接使用GET
以外的方法发出请求,但这不是链接的正常行为,而是依赖于Javascript来制作在幕后提交的表单。 Rails使用jquery-ujs
为此,所以请确保您没有禁用它。
也就是说,POST
请求 是表单的正常行为,因此您也可以尝试使用button_to
,只需将按钮样式设置为链接即可必要的。
<%= button_to "Like", like_recipe_path(@recipe), method: :delete, like: true %>
其他信息:
https://github.com/rails/jquery-ujs
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
答案 3 :(得分:-3)
您需要添加GET路线:
resources :recipes do
member do
get 'like'
post 'like'
end
end