如何使用link_to从控制器调用方法?

时间:2016-04-21 09:53:36

标签: ruby-on-rails ruby link-to

我希望我的应用在点击链接时向current_user添加一本书。我的代码似乎没问题,没有错误但是在用户点击链接后没有任何反应。

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什么都不做?数据库book_users中的表在单击此link_to后没有记录任何内容,但它本身运行良好(我通过控制台检查)。我能做什么?如何让用户通过该方法添加书籍以及如何正确调用此方法?感谢您的每一个帮助,谢谢!

7 个答案:

答案 0 :(得分:1)

  = link_to "  Add to my books", {:controller => "books", action: :add_to_books_i_read, book_id: :book.id}

答案 1 :(得分:1)

link_to中的第一个methodsymbol of HTTP verb,因此您无法在控制器中传递您的功能 要在您的控制器上定义和使用新操作,您需要为see here

定义路由

答案 2 :(得分:1)

由于add_to_books_i_read是一个控制器操作方法,因此您应该更新路由以包含books资源的此操作,以便与Rails以一种资源丰富的方式完成事务的方式很好地协作。您还可以使用此路径的URL帮助程序,如下所示:

/books/:id/add_to_books_i_read

您的代码可能如下所示:

# config/routes.rb
resources :books do
  get :add_to_books_i_read, on: :member
end

# app/controller/books_controller.rb
def add_to_books_i_read
  current_user.books << Book.find(params[:id])
  # redirect or other logic
end

# app/views/books/index.html.haml
ul
  - books.each do |book|
    li
      = link_to "#{book.name}", book_path(book)
      = link_to "Add to my books", add_to_books_i_read_book_path(book)

答案 3 :(得分:1)

还尝试将您的路线组织为(注意会员部分)   

resources :books do 
  member do
    get :add_to_books_i_read
  end
end

并查看rake路由输出中的Prefix Verb列。

答案 4 :(得分:1)

来自@Ioannis Tziligkakis,我编辑了一下。

尝试:

# config/routes.rb
resources :books do
  member do
    get :add_to_books_i_read
  end
end

# app/controller/books_controller.rb
def add_to_books_i_read
  current_user.books << Book.find(params[:id])
  # redirect or other logic
end

# app/views/books/index.html.haml
ul
  - books.each do |book|
    li
      = link_to "#{book.name}", book_path(book)
      = link_to "Add to my books", add_to_books_i_read_book_path(book.id)

答案 5 :(得分:1)

试试这个:

# config/routes.rb
resources :books do
  # just not about method:
  # use get for request, search
  # use put for update
  # use post for create
  # use delete for destroy
  put :add_to_books_i_read, on: :member
end

# app/controller/books_controller.rb
def add_to_books_i_read
  @book = Book.find(params[:id])
  current_user.books << @book
  respond_to do |format|
    format.js
  end
end

# app/views/books/index.html.haml
ul
  - books.each do |book|
    li{:id => "book_#{book.id}"}
      = render "links", book: book

# add field: app/views/books/_links.html.haml
= link_to "#{book.name}", book_path(book)
= link_to "Add to my books", add_to_books_i_read_book_path(book), method: :put, remote: true

# add field: app/views/books/add_to_books_i_read.js.erb
$("#book_<%= @book.id %>").html("<%= j render 'books/links', book: @book %>")

答案 6 :(得分:0)

天啊。有了你的建议帮助,我终于明白了!所以,

控制器:

def add_to_books_i_read
    current_user.books << Book.find(params[:id])
    redirect_to(:back) #this line
end

路线

resources :books do
    member do
      get :add_to_books_i_read
    end
end

索引视图

-books.each do |book|
    li
      = link_to "#{book.name}", book_path(book)
      = link_to  "       Add to my books", add_to_books_i_read_book_path(book)

谢谢大家的帮助!我真的很感激! :)