2014-08-28 33 views
0

我有一个角度的应用程序在轨道上运行,并试图从我的轨道后端与参数获取数据,它总是只返回所有投票。我认为我的问题在我的后端。Rails没有使用我的参数发送数据

它总是调用索引行动,但它不使用的参数,有人请帮助我得到这个工作

这里是服务器输出

Started GET "/api/v1/votes.json?votable_id=129&votable_type=Post" for 127.0.0.1 at 2014-08-28 13:16:51 -0700 
Processing by Api::V1::VotesController#index as JSON 
    Parameters: {"votable_id"=>"129", "votable_type"=>"Post"} 
    Vote Load (0.3ms) SELECT "votes".* FROM "votes" ORDER BY id DESC 
Completed 200 OK in 8ms (Views: 5.2ms | ActiveRecord: 0.3ms) 

这里是我的控制器

module Api 
    module V1 
    class VotesController < ApplicationController 
     respond_to :json 

     def index 
     respond_with(Vote.all.order("id DESC")) 
     end 

     def show 
     respond_with(Vote.find(params[:id])) 
     end 

     def create 
     @vote = Vote.new(vote_params) 
     @vote.save 
     respond_with @vote, location: "" 
     end 

     def update 
     @vote = Vote.find(params[:id]) 
     @vote.update(vote_params) 
     respond_with @vote, location: "" 
     end 

     def destroy 
     respond_with Vote.destroy(params[:id]) 
     end 

    private 
     def vote_params 
     params.require(:vote).permit(:vote, :votable_id, : votable_type, :user_id) 
     end 
    end 
    end 
end 

回答

2

您正在向投票控制器发送GET请求,该请求(没有作为请求路径的一部分的ID)将调用索引操作,如config/routes.rb。如果您希望选择性地从索引中显示投票,则必须在索引操作中选择性地使用params。

def index 
    @votes = Vote.where('votable_id IN (?)', params[:votable_id]) 
       .where('votable_type = ?', params[:votable_type]) 
       .order('id DESC') 
    respond_with(@votes) 
end