2016-04-21 79 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