2016-06-16 7 views
0

세 모델 UserBookList이 있습니다. 그들은 서로 서로 관계가 있습니다. 거의 내가 뭘하려고하는지 사용자 목록에 책을 추가합니다. best_in_place gem을 사용하여 show.html.erb 서적의 사용자 목록에서 책을 업데이트합니다. 상태를 업데이트하려고하면 빈 상태로 돌아갑니다. 나는 그것이 왜 업데이트되지 않는 것인지 완전히 확신하지 못합니다. 어쩌면 누군가가 내 코드를 체크 아웃 할 수 있고 내가 뭔가를 놓치고 있는지 볼 수 있습니다.has_many : through 연관을 사용하고 best_in_place 보석을 사용하여 목록에 책 추가

class List < ActiveRecord::Base 
belongs_to :user 
belongs_to :book 
validates :book, uniqueness: { scope: :user_id, message: "%{attribute} already added" } 
end 

class Book < ActiveRecord::Base 
has_many :lists 
end 

class User < ActiveRecord::Base 
has_many :lists 
has_many :books, through: :lists 
end 

목록 컨트롤러

class ListsController < ApplicationController 
respond_to :html, :json 

def create 
    @book = Book.find params[:book_id] 
    @list = current_user.lists.new list_params 
    @list.save 
end 

def update 
    @book = Book.find params[:book_id] 
    @list = current_user.lists.find(params[:id]) 
    @list.update (list_params) 
    respond_with @list 
end 

def destroy 
    @book = Book.find params[:book_id] 
    @list = current_user.lists.find(params[:id]).destroy 
end 

private 

def list_params 
    params.require(:list).permit(:status, :rating).merge(book_id: @book.id) 
end 
end 

책 컨트롤러

class ListsController < ApplicationController 
def show 
    @book = Book.find params[:id] 
    @list = current_user.lists.new 
end 
end 

책/show.html.erb

<%= best_in_place [@book, @list], :status, type: :select, 
    collection: [["Watching" , "Watching"], 
    ["Completed" , "Completed"], 
    ["On-Hold" , "On-Hold"], 
    ["Dropped" , "Dropped"], 
    ["Plan to Read" , "Plan to REad"]], 
    class: 'small dropdown button' %> 
+0

설명서를주의 깊게 읽으십시오. 책 컨트롤러에 추가해야 할 수도 있습니다. respond_with_bip – mrvncaragay

답변

1

일부 구문 오류가 있습니다 - 아니 괄호 후에는 f ind 메서드를 사용하는 경우 - 귀하의 서적 컨트롤러는 "ListsController"라고합니다.

또한 Book은 List에 속하게됩니다. 더 논리적으로 들립니다. 또한 사용자가 도서 만 소유 한 도서는 사용자 본인이 아니게됩니다.

또 다른 것은 귀하의 서적 컨트롤러에 있습니다. 책을 볼 때 왜 새로운 목록을 만드나요?

최고의 보석에 대해서는 언급 할 수 없습니다. 그러나 몇 가지 사항을 수정 한 후에는 제대로 작동 할 수 있습니다.

+0

내가 틀렸다고 정정하십시오. 책은 목록에 속하며 목록에는 많은 책이 있습니다. – Wesly

+0

올바른 내용입니다. 귀하의 모델 목록은 "possess_to"목록이 아닌 "have_many books"이어야합니다. 그러나 그것은 당신의 방식대로 일할 수도 있습니다. 이것은 단지이 방법으로 논리적입니다. – Maxence