小编Kri*_*son的帖子

Rails 3.1如何为指向用户资源的"我"创建API路由

我在rails中有一组API路由,如下所示

namespace "api" do
   namespace "v1" do
     resources :users do
       resources :posts
       resources :likes
       ...
     end
   end
end
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.我可以GET/api/v1/users/fred_flintstone并检索该用户的所有信息.

我现在要做的是添加"我"(ala facebook)的概念,这样如果用户通过身份验证(fred_flintstone),我还可以执行以下操作

GET/api/v1 /我

GET/api/v1/me/posts

...

我需要两套路线.所以我想使用GET/api/v1/me/posts或GET/api/v1/users/fred_flintstone/posts来获得相同的结果.

我已经完成了路线教程并且已经使用Google搜索,因此指针会像直接答案一样受到赞赏.

编辑:

我所做的工作非常有意义.我使用范围在routes表中创建了第二组条目:

scope "/api/v1/me", :defaults => {:format => 'json'}, :as => 'me' do
  resources :posts, :controller => 'api/v1/users/posts'
  resources :likes, :controller => 'api/v1/users/likes'
  ...
end
Run Code Online (Sandbox Code Playgroud)

然后我添加了一个set_user方法来测试params [:user_id]的存在.我真的在寻找一种干涸的方法.

routes ruby-on-rails-3

7
推荐指数
1
解决办法
1603
查看次数

Nokogiri :: XML.parse应该为换行创建单独的Text节点吗?

我有一个外部工具创建的XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<suite>
  <id>S1</id>
  <name>First Suite</name>
  <description></description>
  <sections>
    <section>
  <name>section 1</name>                    
      <cases>
        <case>
          <id>C1</id>
          <title>Test 1.1</title>
          <type>Other</type>
          <priority>4 - Must Test</priority>
          <estimate></estimate>
          <milestone></milestone> 
          <references></references> 
        </case>                             
        <case>
          <id>C2</id>
          <title>Test 1.2</title>
          <type>Other</type>
          <priority>4 - Must Test</priority>
          <estimate></estimate>
          <milestone></milestone> 
          <references></references> 
        </case>
      </cases>
    </section>
  </sections>
</suite>
Run Code Online (Sandbox Code Playgroud)

从irb,我执行以下操作:(输出被抑制直到最终命令)

> require('nokogiri')
> doc = Nokogiri::XML.parse(open('./test.xml'))
> test_case = doc.search('case').first
=> #<Nokogiri::XML::Element:0x3ff75851bc44 name="case" children=[#<Nokogiri::XML::Text:0x3ff75851b8fc "\n          ">, #<Nokogiri::XML::Element:0x3ff75851b7bc name="id" children=[#<Nokogiri::XML::Text:0x3ff75851b474 "C1">]>, #<Nokogiri::XML::Text:0x3ff75851b1cc "\n          ">, #<Nokogiri::XML::Element:0x3ff75851b078 name="title" children=[#<Nokogiri::XML::Text:0x3ff75851ad58 "Test 1.1">]>, #<Nokogiri::XML::Text:0x3ff75851aa9c "\n          ">, #<Nokogiri::XML::Element:0x3ff75851a970 name="type" children=[#<Nokogiri::XML::Text:0x3ff75851a6c8 "Other">]>, …
Run Code Online (Sandbox Code Playgroud)

ruby nokogiri xml-parsing

1
推荐指数
1
解决办法
905
查看次数

标签 统计

nokogiri ×1

routes ×1

ruby ×1

ruby-on-rails-3 ×1

xml-parsing ×1