I stored all the tablename I've created to Menu table. And every time I add the table in Menu, it will automatically create a link under Menu list
see below.
I want each table in Menu to have a Listing
, New
, Edit
, and Delete
.
see below.
I have a controller prj_menus_controller
, I will just pass the id of the table from Menu table.
here is the code for index and new in my controller.
Class PrjMenusController < ApplicationController
def index
@prj_menus = Menu.find(params[:id]).tablename.singularize.classify.constantize.all
end
def new
@prj_menu = Menu.find(params[:id]).tablename.singularize.classify.constantize.new
end
def create
@prj_menu = Menu.find(params[:id]).tablename.singularize.classify.constantize.new(prj_menu_params)
if @prj_menu.save
redirect_to :action => 'index'
else
render :new
end
end
private
def prj_menu_params
params.require("HERE IS MY PROBLEM").permit(:name)
end
end
and in my new.html.erb
<%= simple_form_for (@prj_menu),:url => prj_menus_path, :method => :post do |f| %>
<%= f.input :name %>
<%= f.submit 'Save', class: 'btn btn-primary' %>
<%= link_to "Cancel", :back, {:class=>"btn btn-default"} %>
<% end %>
I can get the list in my index.html.erb
, it is working. My problem is that I don't know how to get all params when I click the submit in new.html.erb
. I got this hash
{"sample1_table"=>{"name"=>"test 6"}, "commit"=>"Save","controller"=>"prj_menus", "action"=>"create"}
It is correct but I don't know what to put in my controller. I tried this params.require(["#{@prj_menu}"]).permit(:name)
, it creates new record but params[:name] does not save.
I am still a noob to Ruby On Rails and I don't know what to search for this.
I think you are mostly confused on what parameter whitelisting does and how parameters are passed from the form to the controller.
I does not really matter if the name of the form hash matches the name of the database table. It just does in most cases since that makes the most sense. It's simply representative of the REST interface of your app.
Let's say you have a action which creates Pets:
POST /pets
And in our form we have a bunch of inputs like so:
Rails will map create a
params[:pet]
hash{ name: 'Spot' }
. But we want to save the pets as anAnimal
.Animal
does not care what the params key is, it just gets a hash. But we also need to tell simple_form what parameter key we want to use since it looks at themodel_name
attribute.Gives us
pet[name]
instead ofanimal[name]
.I don't get why you are so adamant about making things so difficult for yourself though unless you are creating a database administration tool in the vein of PHP_MyAdmin. And even that case you don't even want to be altering the schema of the app database at runtime.
You are going to run into huge problems when it comes to creating effective queries for getting all the menus.