2016-04-21 65 views
1

我希望我的应用程序在点击链接时将图书添加到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中的表不会记录任何内容,但它本身也很有效(我通过控制台进行了检查)。我能做什么?如何让用户通过该方法添加图书以及如何正确调用此方法?每一个帮助将不胜感激,谢谢!

回答

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) 

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

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

是的,我已经尝试过,但它仍然没有任何东西( – Jakov

+0

你可以检查日志,如果这个动作被称为? –

+0

是的,它可以与管理员和是的,它被称为 – Jakov

1

第一,methodlink_tosymbol of HTTP verb,所以你不能传递你的函数在控制器 定义和使用新的行动,你控制器,你需要定义为see here

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) 
+0

嗯,我做了所有这些,但现在有一个错误,当我在/书页:未定义的方法'add_to_books_i_read_book_path'为#<#:0x00000006f85678>,但它存在于路线文件,你建议。 当我做耙路线它说/books/:book_id/add_to_books_i_read(.:forma t)它看起来很可疑,因为在所有其他行为中,它只是书籍/:id not books /:book_id 您认为什么? – Jakov

+0

嗯..我用'get:add_to_books_i_read,on :: member'更新了代码。你可以再试一次吗? –

1

也尽量安排你的路由作为(注成员部)

resources :books do 
    member do 
    get :add_to_books_i_read 
    end 
end 

和看到在耙路线输出前缀动词柱。

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) 
+0

发生错误 - 模板丢失。我读到它可能会发生,因为创建操作,但我不知道我在哪里叫它( – Jakov

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 %>") 
+0

)发生错误 - 模板丢失。我读它可能会发生,因为创建操作,但我没有得到在哪里( – Jakov

+0

)我知道发生了什么,你想用ajax做还是不行,我可以帮你@Jakov。 – thanhnha1103

+0

我刚刚更新了答案,希望对你有用@Jakov:D – thanhnha1103

相关问题