Rails 中的 RESTful habtm 关系

发布于 2024-12-29 13:55:35 字数 832 浏览 2 评论 0原文

假设我有两个 :has_and_belongs_to_many 模型,它们通过一个简单的索引表连接起来。这就是我的routes.rb的样子:

match "foo/:id" => "foos#create", :via => :post
match "foo/:id" => "foos#update", :via => :put
match "foo/:id" => "foos#read", :via => :get
match "foo/:id" => "foos#delete", :via => :delete

match "foos/:id/bars" => "foos#add_bar", :via => :post

最后一条路线(有问题的路线)映射到foo_controller.rb中的add_bar方法,该方法需要一个bar的JSON表示:

def add_bar
  @bar = Bar.find(params[:bar][:id])
  if @bar.nil?
    @bar = Bar.create(params[:bar])
    validation_error(@bar.errors) unless @bar.valid?
    @bar.save!
  end
  @foo.bars << @bar
  @foo.save!
  respond(ResponseCode::OK, @bar)
end  

这是否使感觉?我正在匹配推送到 Rails 中的集合的行为,但从 RESTful 的角度来看,这对我来说感觉很脏。也许我错了。想法?

So let's say I have two :has_and_belongs_to_many models that are joined by a simple index table. This is what my routes.rb looks like:

match "foo/:id" => "foos#create", :via => :post
match "foo/:id" => "foos#update", :via => :put
match "foo/:id" => "foos#read", :via => :get
match "foo/:id" => "foos#delete", :via => :delete

match "foos/:id/bars" => "foos#add_bar", :via => :post

And the last route (the one in question) maps to a the add_bar method in the foo_controller.rb which expects a JSON representation of a bar:

def add_bar
  @bar = Bar.find(params[:bar][:id])
  if @bar.nil?
    @bar = Bar.create(params[:bar])
    validation_error(@bar.errors) unless @bar.valid?
    @bar.save!
  end
  @foo.bars << @bar
  @foo.save!
  respond(ResponseCode::OK, @bar)
end  

Does this make sense? I'm matching the behavior of pushing to the collections in rails, but it feels dirty to me from a RESTful standpoint. Maybe I'm wrong. Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入怼 2025-01-05 13:55:35

重新开始并使用:

resources :foos do
  member do
    post 'bars', :to => "foos#add_bar", :as => :add_bar_to
  end
end

这为您提供了六个基本的 RESTful CRUD 路由,以及一个 add_bar_to_foo 辅助方法。您的 foos 控制器应该有一个 show 方法,而不是 read。这是 Rails 约定。

要处理添加条形,

def add_bar
  @foo = Foo.find(params[:id]) # find the foo
  @bar = @foo.bars.build(params[:bar]) # build new bar through foo
  if @bar.save
    render :json => @bar # 'OK' response
  else
    render :json => @bar, :status => :unprocessable_entity # failure response
  end
end  

Scratch all of that and use:

resources :foos do
  member do
    post 'bars', :to => "foos#add_bar", :as => :add_bar_to
  end
end

This gives you the six basic RESTful CRUD routes, plus an add_bar_to_foo helper method. Your foos controller should have a show method instead of read. This is Rails convention.

To handle adding bars,

def add_bar
  @foo = Foo.find(params[:id]) # find the foo
  @bar = @foo.bars.build(params[:bar]) # build new bar through foo
  if @bar.save
    render :json => @bar # 'OK' response
  else
    render :json => @bar, :status => :unprocessable_entity # failure response
  end
end  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文