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

发布于 2024-10-31 19:33:27 字数 1134 浏览 0 评论 0原文

我正在将 Thumb_Up gem 用于 ruby​​ on Rails。 https://github.com/brady8/thumbs_up

我希望用户能够对帖子进行投票。 但是,我无法弄清楚如何允许用户单击每个帖子旁边的按钮并向数据库添加投票。

我可以通过执行以下操作在 Rails 控制台中实现此操作:

u=User.first

m=Micropost.first

u.vote_for(m)

但是,当在视图中单击按钮时如何实现此操作。我假设我必须使用 ajax,但是我如何知道需要发布到的 url 才能执行此操作?

任何帮助将不胜感激。

更新:

非常感谢您的帮助!我对下面的代码仍然有问题。

这是我的routes.rb

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

视图:

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

控制器:

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

但是,我仍然收到此错误: 没有路由匹配 {:controller=>"microposts", :id=>#, :action=>"vote"}

有人知道为什么路由不正确匹配吗?

I am using the Thumb_Up gem for ruby on rails.
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:

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?

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

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"}

Would anyone know why the routes aren't matching correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2024-11-07 19:33:27

我假设 Rails 3。Rails 2 的路线看起来有点不同。

首先,您需要在 config/routes.rb 文件中定义一条路由。你可以通过多种方式做到这一点。如果您已经有了微帖子的路由,则可以简单地添加“投票”操作:(

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

为了清楚起见,上面的“帖子”指的是 HTTP POST 方法,与您的微帖子类无关。)如果您使用该路由,然后您需要在 Microposts 控制器中创建一个“投票”方法来捕获它。然后

    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

在您视图的 AJAX POST 调用中(假设我给出的示例路由),您将获得以下网址:

    vote_micropost_path(@micropost)

它看起来像 /microposts/56/vote

I am assuming Rails 3. Rails 2's routes would look a little different.

First you would need to define a route in your config/routes.rb file. 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. 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:

    vote_micropost_path(@micropost)

It would look like /microposts/56/vote

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文