렌더링 작업에 연결된 객체를 포함 할 내 stories#index
액션에 대한 요청을 받고 그 액션에서 검색 매개 변수를 준수하는 스토리의 배열로 구성된 응답을 생성합니다. 나는 current_user
가 있는지 여부와 같은 내 대답에 약간의 추가 데이터, 내 검색이 이야기를 찾았다 통해 날짜 포함 :어떻게 많은 게시물이있는 이야기의 모델이
def index
ajax_response = {}
ajax_response[:currentUser] = user_signed_in? ? current_user : "no current user"
searched_through_date = Stories.last.created_at
@stories = get_stories(params,searched_through_date)
if @stories && @stories.length << 200
ajax_response[:stories] = @stories
ajax_response[:searched_through_date] = searched_through_date
else #only happens if there are too many responsive stories
ajax_response[:error] = {:type => "Response too large", :number_of_stories => @stories.length }
end
render :json => ajax_response
end
지금 내가 응답을 변경하려면를 그 각각의 이야기가 I return에는 추가 특성 인 :latest_post
이 있습니다.이 특성은 해당 스토리에 속한 가장 최근의 게시물로 구성됩니다. 상대적인 nOOb로서, 스토리 오브젝트를 수정하는 데 문제가있어서이 새로운 속성/연관을 포함 시켜서 스토리 오브젝트와 함께 응답의 일부로 렌더링합니다.
도움이 될 것입니다.
EDIT :
여기에 get_stories
방법의 중요한 부분이다
def get_stories(params)
q = get_story_search_params(params)
Story.search_with_params(q).limit(q[:limit]).offset(q[:offset])
end
def get_story_search_params(params)
q = {}
q[:limit] = params[:limit].blank? ? 25 : params[:limit].to_i
q[:text_to_search] = params[:text_to_search].blank? ? nil : params[:text_to_search]
q[:offset] = params[:offset].blank? ? 0 : params[:offset]
return q
end
당신은이다 (당신의'get_stories' 방법의 코드를 포함 아마한다 무엇을 수정하고 싶은지). – MrTheWalrus
코드를 포함하도록 질문을 편집 하겠지만,이 메소드는 단순히 스토리 오브젝트의 배열을 반환합니다. @ MrTheWalrus는 배열의 스토리 객체를 수정하고 싶다는 점을 지적하는 것이 옳습니다. –