Ruby on rails 对于';比如';行动

Ruby on rails 对于';比如';行动,ruby-on-rails,ruby-on-rails-4,routing,Ruby On Rails,Ruby On Rails 4,Routing,我的应用程序中出现了一个无法修复的错误。我一直收到路由错误,没有路由匹配[GET]“/recipes/1/like”。我不明白的问题是,为什么它是作为一个get请求而不是一个帖子 这是我的密码; 路线 配方控制器 def like like = Like.create(like: params[:like], user: @user, recipe: @recipe) if like.valid? flash[:success] = "Your selectio

我的应用程序中出现了一个无法修复的错误。我一直收到路由错误,没有路由匹配[GET]“/recipes/1/like”。我不明白的问题是,为什么它是作为一个get请求而不是一个帖子

这是我的密码; 路线

配方控制器

  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
看法

当我检查按钮时

<a rel="nofollow" data-method="post" href="/recipes/1/like?like=true">
                        <i aria-hidden="true" class="fa fa-thumbs-o-up"></i> &nbsp; 
                        0 
                    </a>


我已经检查了链接,它在呈现时确实有一个post操作,但是当我单击它时,它会将我带到这个路由错误页面。有人能解释一下我哪里出错了吗

使用
链接到

通过链接发送post请求是html
无法做到的事情。您只能使用它发出
GET
请求,而不能使用
POST
。尽管如此,Rails为您提供了一些魔术

通过提供
:method=>:post
选项,Rails将创建一个表单,并通过javascript提交它。请注意,您需要使用
jqueryrailsgem
才能工作,如果您没有它,就不会有任何神奇的事情发生,您的链接将默认为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&amp;password=secret">Create User</a>

我检查了你的github&你在那里启用了jquery-我的浏览器启用了javascript。。。我一开始也遇到了同样的错误-我只是通过将“get”路由添加到
config/routes.rb
项目中,得到了第一个错误

...
      #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.create(like:params[:like],user:@user,recipe:@recipe)

当您有
chef\u id
而不是
user\u id
。。。这意味着你的代码应该是


like=like.create(like:params[:like],chef_id:@user,recipe:@recipe)
(当然,这意味着您必须调整数据库模式等)


当然,除非是喜欢该配方的用户id,否则在这种情况下,在路由问题完成后,需要调整控制器和模式。

As:“支持的动词有:post、:delete、:patch和:put。请注意,如果用户禁用了JavaScript,请求将返回到使用GET。”。您的代码看起来正常,可能是您应该检查您的浏览器设置?您是否检查了浏览器控制台中的链接?它是否有
data method=“post”
?其中控制器yr
方法类似?@hoangdd javascript似乎已启用(它在应用程序的其他地方工作)@Pavan当我检查它时,它确实将post作为数据方法提供给我。我尝试了底部代码,但它似乎没有为我创建任何可点击的内容。它给了我拇指,但没有别的。奇怪的是,我的代码过去一直在工作,但由于不得不从我的github repo克隆,所以出现了随机中断。@J.Schimdt我猜你的repo对gemfile有不同的设置,或者可能是一个github的.ignore文件阻止了它。。。如果您的本地版本正常工作,您应该尝试排除故障,使其在所有机器上传播。有时,一个
捆绑包更新
命令,然后推送更改,与一个
捆绑包安装
相比,会起作用。
谢谢@Mirv,我在routes.rb中添加了“get”这样的选项,似乎可以解决它:)@J.Schimdt如果你觉得这是解决方案,你可以将其标记为已解决;如果有帮助但没有解决方案,你可以直接投票解决:)
<%= 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&amp;password=secret">Create User</a>
# 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 %>
...
      #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
...
 ...
     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
...