简体   繁体   English

您如何使用Rails 3 gem方法来更新数据库模型?

[英]How do you use a Rails 3 gem method to update a database model?

I am using the Thumb_Up gem for ruby on rails. 我正在使用Thumb_Up宝石在轨道上使用红宝石。 https://github.com/brady8/thumbs_up https://github.com/brady8/thumbs_up

I want users to be able to vote on posts. 我希望用户能够对帖子进行投票。 However, I am unable to figure out how I can allow a user to click a button next to each post and add a vote to the database. 但是,我无法弄清楚如何允许用户单击每个帖子旁边的按钮并向数据库添加投票。

I can get this to happen in the rails console through doing the following: 通过执行以下操作,我可以在rails控制台中完成此操作:

u=User.first

m=Micropost.first

u.vote_for(m)

However, how can I get this to happen when a button is clicked in view. 但是,如何在视图中单击按钮时发生这种情况。 I am assuming I would have to use ajax, but how would I know the url I need to post to to make this action occur? 我假设我必须使用Ajax,但是我怎么知道要发布此操作所需的URL?

Any help would be greatly appreciated. 任何帮助将不胜感激。

Update: 更新:

Thanks so much for the help! 非常感谢你的帮助! I am still having a problem with the code below. 我仍然对下面的代码有疑问。

Here is my routes.rb 这是我的routes.rb

resources :microposts do
  post :vote, :on => :member
end

View: 视图:

<%= link_to('vote for this post!', vote_micropost_path(@micropost), :method => :post) %>

Controller: 控制器:

def vote
    @micropost = Micropost.find(params[:id])
  current_user.vote_for @micropost

  # This assumes you'll only call it via AJAX.
  # If your ajax call doesn't return "ok", then you know something went wrong
  render :text => 'ok', :layout => false
end

However, I'm still getting this error: No route matches {:controller=>"microposts", :id=>#, :action=>"vote"} 但是,我仍然收到此错误:没有路由匹配{:controller =>“ microposts”,:id =>#,:action =>“ vote”}

Would anyone know why the routes aren't matching correctly? 有谁知道为什么路由不正确?

I am assuming Rails 3. Rails 2's routes would look a little different. 我假设使用Rails3。Rails2的路线看起来有些不同。

First you would need to define a route in your config/routes.rb file. 首先,您需要在config / routes.rb文件中定义一条路由。 You could do this many ways. 您可以通过多种方式执行此操作。 If you already have a route for microposts, you could simply add a "vote" action: 如果您已经有微博的路线,则只需添加一个“投票”操作即可:

    resources :microposts do
      post :vote, :on => :member
    end

(For clarity, the "post" above refers to the HTTP POST method and has nothing to do with your Micropost class.) If you use that route, you would then need to create a "vote" method in your Microposts controller to catch it. (为清楚起见,上面的“ post”是指HTTP POST方法,与Micropost类无关。)如果使用该路由,则需要在Microposts控制器中创建一个“ vote”方法来捕获它。 。 Something like 就像是

    def vote
      @post = Micropost.find(params[:id])
      current_user.vote_for @post

      # This assumes you'll only call it via AJAX.
      # If your ajax call doesn't return "ok", then you know something went wrong
      render :text => 'ok', :layout => false
    end

Then in your view's AJAX POST call (assuming the example route I gave), you would get the url with: 然后在视图的AJAX POST调用中(假设我给出的示例路由),您将获得带有以下内容的网址:

    vote_micropost_path(@micropost)

It would look like /microposts/56/vote 看起来像/ microposts / 56 / vote

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Rails模型中包含gem的class方法? - How do I include a gem's class method in a Rails model? 如何在不使用不同变量名的情况下更新 rails model 实例方法中的属性? - How do you update attributes in an instance method in a rails model without using different variable names? 如何配置 Rails Gem (TrueMail) - How do you configure a Rails Gem (TrueMail) Rails 3:如何在不更改Gem源的情况下向Gem模型添加方法 - Rails 3: How to add a method to the Gem model without changing the Gem source 你如何使用react-rails react_component与webpacker gem rails 5.1? - how do you use react-rails react_component with webpacker gem rails 5.1? 如何在Rails中测试模型是否具有给定方法? - How do you test whether a model has a given method in Rails? 如何在不安装gem的情况下以漂亮的格式输出Ruby on Rails控制台数据库结果集? - How do you output Ruby on Rails console database resultsets in a pretty format without installing a gem? 使用Rails和Rspec,你如何测试数据库没有被某个方法触及 - Using Rails and Rspec, how do you test that the database is not touched by a method 如何在Rails模型和form_for中使用自定义函数? - How do you use a custom function in Rails Model and form_for? 如何从 Rails 中的 model 方法保存到数据库 - How do I save to database from a model Method in rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM