我的应用中出现了一个我似乎无法解决的错误。我一直得到路由错误没有路由匹配[GET]“/ recipes / 1 / like”。我无法弄清楚的问题是,为什么它作为一个获取请求而不是一个帖子?
这是我的代码; 路由
#recipe routes
resources :recipes do
collection do
get 'search'
end
member do
post 'like'
post 'review'
end
resources :reviews, except: [:show, :index]
end
食谱控制器
def like
like = Like.create(like: params[:like], user: @user, recipe: @recipe)
if like.valid?
flash[:success] = "Your selection was successful"
redirect_to :back
else
flash[:danger] = "#{@user} " + 'you can only like/dislike once per item.'
redirect_to :back
end
end
视图
<div class="pull-right">
<%= link_to like_recipe_path(@recipe, like: true), method: :post do %>
<i aria-hidden="true" class="fa fa-thumbs-o-up"></i>
<%= @recipe.thumbs_up_total %>
<%end %>
<%= link_to like_recipe_path(@recipe, like: false), method: :post do %>
<i aria-hidden="true" class="fa fa-thumbs-o-down"></i> <%= @recipe.thumbs_down_total %>
<%end %>
</div>
rake routes
like_workout_path POST /workouts/:id/like(.:format) workouts#like
like_recipe_path POST /recipes/:id/like(.:format) recipes#like
当我检查按钮时
<a rel="nofollow" data-method="post" href="/recipes/1/like?like=true">
<i aria-hidden="true" class="fa fa-thumbs-o-up"></i>
0
</a>
我已经检查了链接,并且在渲染时确实有一个帖子的动作,但是当我点击它时,它会将我带到此路由错误页面。有人可以解释我哪里出错了吗?
答案 0 :(得分:0)
使用link_to
进行发布
通过链接发送帖子请求是html <a href>
无法做到的事情。您只能使用它来发出GET
个请求,而不是POST
。话虽这么说,Rails有一些神奇的技巧。
通过提供:method => :post
选项,Rails将通过 javascript 创建表单并提交。请注意,您需要jquery-rails gem
才能使用此功能,如果您没有,则不会发生任何魔术,您的链接将默认为GET请求。
<%= link_to "Post", root_path, :method => :post %>
# => <a rel="nofollow" data-method="post" href="/">Post</a>
#Adding more params to the POST request
<%= link_to "Create User", users_path(:email => "jdoe@email.com", :password => "secret"), :method => :post %>
# => <a rel="nofollow" data-method="post" href="/users?email=jdoe%40email.com&password=secret">Create User</a>
或者您可以使用form_tag
# not tested, maybe will have syntax issue
<%= form_tag(like_recipe_path, {:method => :post}) do %>
<%= contant_tag(:i, nil, class: "fa fa-thumbs-o-up", "aria-hidden": true, :onclick => "this.form.submit();") %>
<%= @recipe.thumbs_up_total %>
<% end %>
答案 1 :(得分:0)
我检查了你的github&amp;你在那里启用了jquery - 我的浏览器启用了javascript ...我首先得到了同样的错误 - 我通过向项目的config/routes.rb
添加'get'路由得到了第一个错误... < / p>
...
#recipe routes
resources :recipes do
collection do
get 'search'
end
member do
match 'recipes/:id' => 'recipes#like', via: [:get, :post]
match 'review/:id' => 'recipes#review', via: [:get, :post]
end
resources :reviews, except: [:show, :index]
end
...
这为更多错误和问题打开了大门......例如在schema.rb
的桌面上......
...
create_table "likes", force: :cascade do |t|
t.boolean "like"
t.integer "chef_id"
t.integer "recipe_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "workout_id"
end
...
...当然,它仍会在您的食谱控制器中为您正在使用的like
操作留下编程风格...
like = Like.create(like: params[:like], user: @user, recipe: @recipe)
如果您有chef_id
而不是user_id
...这意味着您的代码应该是......
like = Like.create(like: params[:like], chef_id: @user, recipe: @recipe)
(当然这意味着您必须调整数据库架构等)
除非用户喜欢配方的ID,否则在路由问题完成后需要调整控制器和架构。