Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement a 'create' action without 'new' action

How can I best implement this feature: As an admin, I can assign a Resident Manager to a Hall.

  • I have a User model with a namespace routing for the admin -I intend on having another namespace routing that would hold the functions of the RM-

  • I have a Hall model.

  • Since its a many-many relationship between the above to models, I have a Management join model which contains only user_id and hall_id columns.

I know implementing the above feature, entails creating a new record in the management table but I don't know how to do it. I didn't think using a form (management#new) would solve this because the admin should not know the user_ids/hall_ids...

BELOW IS WHAT I HAVE TRIED TO DO BUT I CAN'T GET IT RIGHT

When the admin gets to the user index page, s/he should see a link for Hall Assignment for each user. This link leads to the management show page for that particular user, which would show the list of halls assigned to that user and also the show all the other remaining halls that isn't assigned to the user. So, either clicking an ADD button or on the hall's name should add it to that user's assigned halls list which is on the same page.

Management#show page

<h2><%= @user.email %>'s assigned halls</h2>

<% @user.managements.each do |management| %>
  <%= management.hall.name %>
<% end %>

<p> HALL LISTS </p>

<ul>
  <% @halls.each do |hall| %>
    <li><%= hall.name %> <%= button_to "Add" %> </li>
  <% end %>
</ul>

Here's is my Management controller

class Admin::ManagementsController < ApplicationController
  def index
    @managements = Management.all
  end

  def show
    @user = User.find(params[:id])
    @halls = Hall.all
  end

  def create
    @management = Management.create(managements_params)

    redirect_to admin_management_path
  end

  private 

  def managements_params 
    params.
      require(:management).
      permit(user_id: params[:user_id], hall_id: params[:hall_id])
  end
end

And here's a piece of what my routes file looks like:

 namespace :admin do
   resources :users, only: [:index, :update]
   resources :halls, only: [:index, :new, :create]
   resources :managements, only: [:index, :new, :create, :show] do
     resources :halls, only: [:index]
   end
 end
like image 722
Ifeanyi Kalu Avatar asked Dec 17 '25 18:12

Ifeanyi Kalu


1 Answers

Your "add" button is just a mini form (with mostly hidden fields). You can instead just make it an actual form (with the submit-button having the text "Add") and the id-values filled in from the item on the page... it just points to the same routes that you'd normally point the form that you'd find in the new template.

if you want more detail, then show us the code that you have written (rather than a verbal description of it).

Edit:

Ok, so you'd put a button on the page like this

<ul>
  <% @halls.each do |hall| %>
    <li><%= hall.name %> <%= button_to "Add", managements_path(management: {user_id: @user.id, hall_id: hall.id}, method: :put ) %> </li>
  <% end %>
</ul>

Notice the managements_path - you might need to check that that routing is correct (check it against what is in rake routes). Note that you're passing in the user id and the hall id, and that you must set the method to "put" on the button.

like image 120
Taryn East Avatar answered Dec 19 '25 10:12

Taryn East



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!