Rails respond_to格式HTML Json不能使用params

时间:2017-11-19 05:24:44

标签: html ruby-on-rails angularjs json format

我正在创建一个Rails-Angularjs RESTful应用程序。 rails控制器索引动作需要为Angularjs前端生成json。索引操作接收一个参数,该参数必须在查询中用作过滤器。如果我不使用这些参数,一切都会显示角度显示结果。但是,如果我将收到的参数添加到查询中,它就不起作用。

这个动作索引使用查询中的参数:      def指数         @project_id = params [:project_id]         @tickets = PrintingTicket.where(project_id:@project_id)

    respond_to do |format|
      format.html
      format.json { render json: @tickets }
    end
 end

这是不使用参数来过滤查询中的信息的操作索引:

 def index
    @project_id = params[:project_id] 
    @tickets = PrintingTicket.all

    respond_to do |format|
      format.html
      format.json { render json: @tickets }
    end
 end

我注意到索引操作执行了两次,一次用于HTML,一次用于JSON。 Project_id在首次运行时包含有效信息,广告为“无”字样。第二次运行。

通过观察控制台,在两种情况下,它都会在开头Parameters: {"project_id"=>"5982a8799f911d0318054997"}显示参数,但是,在我使用参数的情况下,参数有' nil'最后,正如您在日志中看到的那样:

    Started GET "/projects/5982a8799f911d0318054997/tickets" for ::1 at 2017-11-18 21:50:34 -0800
Processing by TicketsController#index as HTML
  Parameters: {"project_id"=>"5982a8799f911d0318054997"}
  Rendered tickets/index.html.erb within layouts/application (0.1ms)
MONGODB | localhost:27017 | ticketmongo_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>BSON::ObjectId('595f189d9f911d0518244d9b')}, "limit"=>1, "singleBatch"=>true}
MONGODB | localhost:27017 | ticketmongo_development.find | SUCCEEDED | 0.0019340000000000002s
MONGODB | localhost:27017 | ticketmongo_development.find | STARTED | {"find"=>"companies", "filter"=>{"_id"=>BSON::ObjectId('596059589f911d047bd195bf')}}
MONGODB | localhost:27017 | ticketmongo_development.find | SUCCEEDED | 0.001964s
  Rendered companies/_company.html.erb (3.5ms)
  Rendered projects/_projects.html.erb (0.5ms)
  Rendered layouts/_topmenusignin.html.erb (7.1ms)
Completed 200 OK in 120ms (Views: 106.0ms)


Started GET "/tickets/index/tickets.json" for ::1 at 2017-11-18 21:50:34 -0800
Processing by TicketsController#index as JSON
  Parameters: {"id"=>"tickets"}
MONGODB | localhost:27017 | ticketmongo_development.find | STARTED | {"find"=>"printing_tickets", "filter"=>{"project_id"=>nil}}
MONGODB | localhost:27017 | ticketmongo_development.find | SUCCEEDED | 0.0016179999999999999s
Completed 200 OK in 4ms (Views: 3.1ms)

任何帮助都会受到欢迎!!

1 个答案:

答案 0 :(得分:0)

GET "/projects/5982a8799f911d0318054997/tickets"

您可以从rake routes tickets#index /projects/:project_id/tickets行动的路线parameters[:project_id]看到5982a8799f911d0318054997request.bodyGET "/tickets/index/tickets.json" 。如果您要传递其他参数,它们将位于parameters[:id] = 'tickets

GET '/tickets/index/:id'

这条路线没有意义。您需要检查路由器,因为它正在使用GET request',可能是您错误地设置了路由。

现在看起来像这样

/tickets/index/tickets.json

当您对网址tickets执行id时,您的最后一次输入int nnodes; int main(){ FILE * file = fopen("pub08.in", "r"); int nkeys; fscanf(file, "%d %d", &nnodes, &nkeys); long int graphsize = nnodes * nnodes; long int * graph = malloc(graphsize * sizeof (long int)); for (int i = 0; i < graphsize; i++) { graph[i] = IN; } for (int i = 0; i < nnodes; i++) { long int a, b, prize; fscanf(file, "%ld %ld %ld", &a, &b, &prize); graph[a * nnodes + b] = prize; graph[b * nnodes + a] = prize; } } 将作为100000 10000 61268 56095 10 40567 20917 17 97937 47973 13 74088 21826 13 62183 30464 11 97793 80708 12 35121 90180 10 77067 97297 17 4657 33995 16 88147 42709 18 95937 25936 19 79853 24452 11 9677 36288 11 91869 48767 15 34585 46478 17 41874 40622 15 13700 19942 18 15660 79277 14 ... 参数