我希望我的应用程序在点击链接时将图书添加到current_user。我的代码似乎没问题,没有错误,但用户点击链接后没有任何反应。如何使用link_to从控制器调用方法?
book.rb:
has_many :book_users
has_many :users, through: :book_users
user.rb:
has_many :book_users
has_many :books, through: :book_users
book_user.rb:
belongs_to :book
belongs_to :user
books_controller.rb:
before_action :is_admin?, except: [:book_params, :add_to_books_i_read, :index]
before_filter :authenticate_user!
expose(:book, attributes: :book_params)
expose(:books)
def create
if book.save
redirect_to(book)
else
render :new
end
end
def update
if book.save
redirect_to(book)
else
render :edit
end
end
def add_to_books_i_read(book_id)
current_user.books << Book.find(book_id)
end
在我的索引视图中我有
ul
-books.each do |book|
li
= link_to "#{book.name}", book_path(book)
= link_to " Add to my books", {:controller => "books", method: :add_to_books_i_read, book_id: :book.id}
那么,我做错了什么?为什么我的方法add_to_books_i_read什么都不做?点击这个link_to后,数据库book_users中的表不会记录任何内容,但它本身也很有效(我通过控制台进行了检查)。我能做什么?如何让用户通过该方法添加图书以及如何正确调用此方法?每一个帮助将不胜感激,谢谢!
是的,我已经尝试过,但它仍然没有任何东西( – Jakov
你可以检查日志,如果这个动作被称为? –
是的,它可以与管理员和是的,它被称为 – Jakov