轨道如何将关联模型与admin名称空间一起使用 [英] rails how to use Associated Model with the admin namespace

查看:56
本文介绍了轨道如何将关联模型与admin名称空间一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢我可以在这里提问.我正在使用Rails 5.1.4和ruby 2.5.0.我有两个模型,分别是模型A和模型B.

thanks that i can ask questions here. I'm using rails 5.1.4 with ruby 2.5.0. I have two models the model A and Model B.

    Model A has_many bs
    Model B belongs_to a

管理员用户可以生成新的a条目,并且他可以生成新的b条目.no-admin-user可以显示,索引或保留.

The admin-user can generate new a-entries and he can generate new b-entries. The no-admin-user can show, index or reserve.

routes.rb

    resources :as, only: [:index, :show] do
      resources :bs, only: [:index, :show, :reserve]
    end
    namespace :admin do
     resources :as, only: [:create, :edit, :update, :destroy,  
       :show] do
       member do
         post :activate
         get :activate
         post :deactivate
         get :deactivate
       end
     resources :bs, only: [:create, :edit, :update, :destroy, :show]
    end
  end

app/views/admin/as/show.html.erb 中,您可以看到a的DB Entry的值.因此,现在我的想法是认识到可以使用表单添加b的新B条目.我尝试了这个show.html.erb

In the app/views/admin/as/show.html.erb you can see the values of the DB Entry of an a. So now my idea is to realise that you can add new B Entries of b with a form. I tried this show.html.erb

show.html.erb

    <h2><%=t("show")%></h2>
    <p>
      <strong><%=t("title")%></strong>
      <%= @a.title %>
    </p>
    <p>
      <strong><%=t("description")%></strong>
      <%= @a.description %>
    </p>
    <p>
      <strong><%=t("email")%>:</strong>
      <%= @a.email %>
    </p>
    <h2><%=t("entries")%></h2>
    <%= render @a.bs %>
    <h2><%=t("addentry")%></h2>
    <%= form_with(model: [ @a, @admin.bs.build ], local: true) do |f| %>
    <p>
      <%= f.label :title %><br>
      <%= f.text_field :title %>
    </p>
    <p>
      <%= f.label :description %><br>
      <%= f.text_area :description %>
    </p>
    <p>
      <%= f.submit %>
    </p>

将生成表单,但是html源代码不显示"admin-route-path".但是我怎么办呢?

The form will be generated, but the html-sourcecode don't show the "admin-route-path". But how do i this?

   <form action="/as/1/bs" accept-charset="UTF-8" method="post"

我如何更改form_with部分,以便可以将条目从b添加到关联的模型中?

How have i to change the form_with part, that i can add entries into the associated model from b?

推荐答案

似乎您希望表单点击网址/admin/as/bs .要建立指向命名空间控制器的链接,可以使用以下形式:

Seems you want your form to hit the url /admin/as/bs. To build links to a namespaced controller you can use this forms:

link_to [:admin, @a, @b], 'A admin show'
link_to admin_as_bs_path(@a, @b), 'A admin show'

同样适用于表格

form_for [:admin, @a, @b] do ...
form_for @a, url: admin_as_bs_path(@a, @b)

路由助手的名称可能不正确.检查耙路的输出.

The name of the route helper might be wrong. Check rake routes's output.

这篇关于轨道如何将关联模型与admin名称空间一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆