0

railscast 238을 기반으로 작동하도록 댓글을 달았지만 투표 및 평점을 추가하고 싶습니다. 문서 컨트롤러에 대한동일한 컨트롤러에 평점 및 댓글 추가

내 현재 쇼는 다음과 같습니다

def show 
    @article = Article.find(params[:id]) 
    @commentable = @article 
    @comments = @commentable.comments 
    @comment = Comment.new 


    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @article } 
    end 
    end 

나는 동일한 프로세스를 복제 해 평가할 수있는 추가 시도했지만 나는 show 액션에 통합하는 방법을 모르겠어요. 나는 응용 프로그램 컨트롤러에서 보편적 인 함수를 생성하는 것을 고려한 다음, show 함수에서 여러 가지 (newb)를 할당하려고 시도했지만 복잡해 보입니다. 그것은 작동하지 않았다

@rateable = @article 
@ratings = @rateable.ratings 
@rating = Rating.new 

:

나는 추가했습니다. 하지만 지금은 내가 그것을 할당하면 작동할지 모르겠다

@ratings = Article.find(params[:id]} 
@ratings = @rateable.ratings 
@rating = Rating.new 

그래도 작동하는 경우에도 이것을 수행하는 더 깨끗한 방법이 있어야합니다.

편집 : 작업 주석 버전과 동일 코드 줄에서 보정 후

오류가 발생했습니다.

<%= form_for [@rateable, @rating] do |f| %> 
    <% if @rating.errors.any? %> 
    <div class="error_messages"> 
     <h2>Please correct the following errors.</h2> 
     <ul> 
     <% @rating.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.text_area :content, rows: 8 %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
+0

'@ rateable','@ ratings' 및'@ rating' 매개 변수를 모두'show' 뷰에서 사용할 수 있도록 하시겠습니까? –

+0

또한 왜'@commentable = @ article'과'@rateable = @ article'을 설정해야합니까? 왜'@ article'을 참조하지 않는 것일까 요? –

+0

나는'방법 article_ratings_path '' https://github.com/railscasts/154-polymorphic-association-revised/blob/master/blog-after/app/controllers/articles_controller.rb 를 정의되지 얻을 할 때 나는 꽤 많이 벗어나고 믹스에 다른 오브젝트를 추가하는 방법을 모른다. 첫 번째 질문에 대한 대답은 '예'입니다. – McDoku

답변

1

좋아, 생각해 냈어. 당신은 또한 put '/ratings/create' => 'ratings#create'하거나 resources :ratings

폼 나는 당신이 원하는 것을 생각 ratings#create 제출이 의지 경로를해야합니다 당신의 routes.rb 파일에서

<%= f.submit 'Submit', { controller: 'ratings', action: 'create', method: 'post' } %> 

: 그냥에 <%= f.submit %>을 변경합니다.

+0

등급을 포함하는 경로와 경로를 변경하는 것이 승리했습니다! – McDoku

+0

감사합니다. 경로 정보를 포함하도록 답변을 업데이트했습니다. –