0

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

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

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>

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"

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

amarradi
  • 109
  • 3
  • 16
  • Have a look at [`nested_attributes_for`](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html). – ichigolas Mar 19 '18 at 15:15
  • I have an mistake in my posted form_with. My SC of this code is Yes i know `nested_attributes_for`, but – amarradi Mar 19 '18 at 15:47
  • I have an mistake in my posted form_with. My SC of this code is not "@admin" it is `<%= form_with(model: [ @a, @a.bs.build ], local: true) do |f| %>` I tried it like the getting starteg guide http://guides.rubyonrails.org/getting_started.html#rendering-partial-collections Yes i know `nested_attributes_for`, but what have i to change in the `form_with`? – amarradi Mar 19 '18 at 15:56
  • 2
    Possible duplicate of [Rails: form\_for namespaced resource](https://stackoverflow.com/questions/10953174/rails-form-for-namespaced-resource) – ichigolas Mar 19 '18 at 16:00
  • not really, the `form_for` is deprecated. http://api.rubyonrails.org/v5.1/classes/ActionView/Helpers/FormHelper.html#method-i-form_with so i tried it with form_with, but how – amarradi Mar 19 '18 at 16:21

3 Answers3

1

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'

The same is applicable to forms

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.

ichigolas
  • 7,595
  • 27
  • 50
  • i want hit the form `/admin/as/id/bs` to create an new entry of bs – amarradi Mar 19 '18 at 16:27
  • if you try the guide it works fine, but without admin namespace. – amarradi Mar 19 '18 at 16:28
  • Now i found the next step of my solution. Im using form_with, because form_for is soft-deprecated. `<%= form_with(model: [:admin, @a ]) do |f| %> ` This builds the url `form action="/admin/a/id" accept-charset="UTF-8" method="post">` But how do i get the bs in the url. Is this an extra question on stackverflow? – amarradi Mar 20 '18 at 13:03
0

To get route that you are looking for you need to have nested resources. You can find a great explanation here.

0

I found the solution for the associated model and it works fine. Can anyone confirm this. Thanks

<%= form_with(model: [:admin, @a,B.new ], local: true) do |f| %> <% end %>

amarradi
  • 109
  • 3
  • 16