Params + Acts_as_votable

时间:2013-11-25 22:22:10

标签: ruby-on-rails

我仍然遇到重大问题,弄清楚为什么它没有通过参数。他们在我下面显示的强大的参数中,但无论我做什么,它仍然没有通过它们。

真的很感激任何反馈:

book.rb

def upvote

  @book = Book.find(params[:book_id])
  @book.liked_by current_user
  redirect_to books_path
end

def downvote
  @book = Book.find(params[:book_id])
  @book.downvote_from current_user
  redirect_to books_path
end

PARAMS

def book_params
  params.require(:book).permit(:title, :size, :cat, :user_id, :votable, :voter, :vote_scope) 
end

日志中的错误:

Started PUT "/books/1/like" for 127.0.0.1 at 2013-11-25 22:13:14 +0000
Processing by BooksController#upvote as HTML
  Parameters: {"authenticity_token"=>"m+/mqdx3cfcn65TPyTgyzlm2nWCgJ4QY/b6botgrySo=", "book_id"=>"1"}
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
  Book Load (0.5ms)  SELECT "books".* FROM "books" WHERE "books"."id" = $1 LIMIT 1  [["id", "1"]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."voter_id" = 1 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = 'User'  [["votable_id", 1], ["votable_type", "Book"]]
WARNING: Can't mass-assign protected attributes for ActsAsVotable::Vote: votable, voter, vote_scope
    app/controllers/books_controller.rb:92:in `upvote'
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
Redirected to http://0.0.0.0:3000/books
Completed 302 Found in 36ms (ActiveRecord: 4.4ms)

我的模型中的行为是可投票的,但它不会保存给用户。

1 个答案:

答案 0 :(得分:0)

我是一个新手编程和Rails,但是现在我使用的是acts_as_votable,它对我有用。

我从来没有为muy可投票模型创建一个upvote或downvote方法(答案)。我只是在我的路线上包含资源,然后为它创建链接。

对你来说,就像是:

# routes.rb
  ...
  resources: :books do
    member do
      get :upvote, :downvote, (maybe you also want:), :unvote
    end
  end
  ...

随着这个问题的解决,你需要投票的是:

<%= link_to "Upvote", upvote_answer_path(@answer) %>

Downvote和Unvote是相同的,只需检查 rake路线,您就会找到它们。

那应该达到你的目标,避免创造新的方法,顺便说一句,我不知道它是否比我的方法更好,但我的工作。我很乐意从Rails安全专家那里读一篇关于它的评论(因为,从这个意义上说,在Book.rb上创建一个方法可能恰好是更好的选择......)。

祝你好运。