加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何管理嵌套资源和非嵌套资源

发布时间:2020-12-16 21:02:19 所属栏目:百科 来源:网络整理
导读:我想知道什么是解决以下问题的正确方法. 我有两个型号: Publisher and Issue. Publisher has_many issues. 我想管理发布者列表和问题列表中的问题. 例如 : 在发布者列表中,用户可以单击每个发布者附近的“问题”链接.然后他进入问题列表,但只过滤适当的发
我想知道什么是解决以下问题的正确方法.

我有两个型号:

Publisher and Issue. Publisher has_many issues.

我想管理发布者列表和问题列表中的问题.

例如 :

>在发布者列表中,用户可以单击每个发布者附近的“问题”链接.然后他进入问题列表,但只过滤适当的发布者.他可以点击“创建新问题”并转到表单添加新问题.在此表单上,我不需要向他显示选择发布者的选择列表
>在问题列表中,用户可以单击“创建新问题”并转到表单,但这次他应该从select中选择发布者,这与创建的问题有关.

简单地说,我需要单独的问题和出版商问题的crud行动.

首先我尝试制作一个:

resources :issues

  resources :publishers do
    resources :issues
  end

并在问题控制器:

before_filter :find_issue

def find_issue
 @publisher = Publisher.find(params[:publisher_id]) if params[:publisher_id]
 @issues = @publisher ? @publisher.issues : Issue
end

但我必须在我的观点和控制器中做出很多条件.

例如,如果问题是从发布者创建的,则成功时我想重定向到publisher_issues_path而不是issue_path,反之亦然.与“返回列表”等类似的所有链接都存在同样的问题.所以代码在我看来不是很透明.

现在我想知道使用命名空间.

namespace :publishers,do
  resources :issues
end

和做

# app/controllers/publishers/issues_controller.rb
module Publishers
  class IssuesController < ApplicationController
    # actions that expect a :publisher_id param
  end
end

# app/controllers/issues_controller.rb
class IssuesController < ApplicationController
  # your typical actions without any publisher handling
end

并为两个控制器操作创建单独的视图.

是否有更好或更清洁的方法来解决这类问题?我想让我的代码尽可能干.
非常感谢您的回复.

解决方法

路线:
resources :issues
resources :publishes do
  resources :issues
end

控制器:

class IssuesController < ApplicationController
  before_filter :build_issue,only: [:new,:create]
  before_filter :load_issue,only: [:show,:edit,:update,:destroy]

  def index
    @issues = issues.page(params[:page])
  end

  ... all other actions will have access to @issue

private
  def issues
    if params[:publisher_id].present?
      Publisher.find(params[:publisher_id]).issues
    else
      Issue
    end  
  rescue ActiveRecord::RecordNotFound
    redirect_to issues_path(alert: 'Publisher not found')
  end

  def build_issue
    @issue = issues.new(issue_params)
  end

  def load_issue
    @issue = issues.find(params[:id])
  rescue ActiveRecord::RecordNotFound
    redirect_to issues_path(alert: 'Issue not found')
  end

  def issue_params
    # whitelisted attributes goes here
  end
end

为避免使用条件,请使用操作而不是完整的命名路径,即:

redirect_to action: :index
link_to 'Issues',{action: :index}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读