gpt4 book ai didi

ruby-on-rails - (Rails 4.2) 如何从 View 触发脚手架生成?

转载 作者:数据小太阳 更新时间:2023-10-29 07:59:05 37 4
gpt4 key购买 nike

如何从 View 中触发脚手架生成?

例如,假设我有这样的方法:

def scaffold_generation
system "rails g scaffold TodoList task author"
end

如何让“example_page.html.erb”上的按钮触发此方法以在服务器上执行命令? (这里不用担心安全问题)

最佳答案

1:使用 Form Tag 创建表单

<%= form_tag('/create_scaffold') do -%>
<div><%= submit_tag 'Create Scaffold' %></div>
<% end %>

2:编写路由以匹配传入请求。

 match '/create_scaffold', to: 'examples#scaffold_generation', via: :all

3:

class ExamplesController < ApplicationController

def scaffold_generation
system "rails g scaffold TodoList task author"
system "rake db:migrate" #=> use this so that, it won't throw any errors.
render :text => "Whoa !!! Done"
end
end

关于ruby-on-rails - (Rails 4.2) 如何从 View 触发脚手架生成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34591722/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com