【发布时间】:2015-06-22 08:53:15
【问题描述】:
我将创建的所有表名存储到菜单表。而且每次我在Menu中添加表格时,它都会在Menu list
下自动创建一个链接见下文。
我希望Menu中的每个表都有Listing、New、Edit和Delete。
见下文。
我有一个控制器prj_menus_controller,我将只从菜单表中传递表的 ID。
这是我的控制器中 index 和 new 的代码。
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
在我的 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 %>
我可以在我的index.html.erb 中获取列表,它正在工作。我的问题是,当我单击new.html.erb 中的提交时,我不知道如何获取所有参数。我得到了这个哈希
{"sample1_table"=>{"name"=>"test 6"}, "commit"=>"Save","controller"=>"prj_menus", "action"=>"create"}
这是正确的,但我不知道在我的控制器中放什么。我试过这个params.require(["#{@prj_menu}"]).permit(:name),它会创建新记录,但 params[:name] 没有保存。
我仍然是 Ruby On Rails 的菜鸟,我不知道该搜索什么。
【问题讨论】:
-
你试过
params.require(@prj_menu).permit(:name)吗? -
是的,我得到了
params.require(:prj_menu).permit(:name)而不是params.require(:sample_table).permit(:name)
标签: ruby-on-rails ruby-on-rails-4 controller metaprogramming dynamic-programming