提交表格(无模型)并且无法让控制器发挥其作用 [英] Submitting a form (no model) and having trouble getting the controller to play its part

查看:136
本文介绍了提交表格(无模型)并且无法让控制器发挥其作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为webapps创建了一个基本框架(一些静态页面,用户认证,rspec的单元/集成测试)。我想用这个作为未来webapps的基础,但是我需要设置一个方法,在从github克隆它之后重命名它。我在这里获得了一些帮助来生成重命名的代码],但我正在努力弄清楚如何整合它。

I’ve created a basic framework for webapps (some static pages, user authentication, unit/integration testing with rspec). I’d like to use this as a foundation for future webapps, but I need to setup a way to rename it after cloning it from github. I got some help generating the renaming code here]1, but I'm struggling to figure out how to integrate it.

我原来写了rakefile中的重命名代码,但现在我想可能它应该在控制器中。不幸的是,我一直无法让我的代码工作。我有一个允许用户为应用程序输入新名称的视图。这个想法是,用户将克隆框架回购,CD到框架目录,启动rails服务器,然后去他们的浏览器本地主机重命名从那里的文件。但假设启用该视图的视图不起作用。

views/namer/new/html.erb <h1>Rails Framework</h1> <%= form_tag "/namer" do %> <%= text_field_tag "appname" %> <%= submit_tag "Name Your App" , :action => 'create' %> <% end %>

I can't get the "submit" action to work to work properly.  Here's what my controller looks like.

我无法使提交操作正常工作。这是我的控制器的样子。

controllers/namer_controller.rb class NamerController < ApplicationController def index render('new') end def new end def create @appname = Namer.new(params[:appname]) #first, change any instances of the term "framework" to the new name of the app file_names = ['config/environments/test.rb', 'config/environments/production.rb', 'config/environment.rb'] file_names.each do |file_name| text = File.read(file_name) File.open(file_name, "w") { |file| file << text.gsub("Framework", @appname) } end #next,change the root path away from namer#new file_name ='config/routes.rb' text = File.read(file_name) File.open(file_name, "w") { |file| file << text.gsub("namer#new", "pages#home") } flash[:notice] = "Enjoy your app." render('pages/home') end end

Any idea what I'm doing wrong?

任何想法我做错了什么?

Also, it the controller really the best place for the "renaming" code?

另外,它控制器真的是重命名代码的最佳位置?

edit: here's my routes.rb file.

编辑:这是我的routes.rb文件。

Framework::Application.routes.draw do resources :users resources :sessions, :only => [:new, :create, :destroy] match '/signup', :to => 'users#new' match '/signin', :to => 'sessions#new' match '/signout', :to => 'sessions#destroy' match '/contact', :to => 'pages#contact' match '/about', :to => 'pages#about' match '/help', :to => 'pages#help' root :to => "namer#new" match ':controller(/:action(/:id(.:format)))'

UPDATE: I've changed my code in a few ways. 

更新:我用几种方法更改了我的代码。
$ b

  • 创建一个更名模型。 Models / renamer.rb只是类别名称,然后是结束。 (我删除了< Base :: ActiveRecord,因为没有涉及数据库。)

  • 为重命名代码创建了一个rake文件。它只是叫做renamer.rake。

    1. Routes: added post '/namer' => 'namer#create'
    2. Created a Renamer model. Models/renamer.rb is just "class Namer" and then "end." (I removed the "< Base::ActiveRecord" because there's no database involved.)
    3. Created a rake file for the renaming code. It just called "renamer.rake".

    事情看起来不错,但我仍然在寻找一种叫耙来自控制器的文件。任何建议?

    Things are looking good, but I'm still looking for a way to call the rake file from the controller. Any suggestions?

    更新2:这是我的控制器修改后的Create方法。

    UPDATE 2: Here's my revised Create method for my controller. Now the renaming code is here instead of in a rake file.

    def create
      @appname = Namer.new(params[:appname])
      file_names = ['config/environments/test.rb', 'config/environments/production.rb', 
        'config/environment.rb']
      file_names.each do |file_name|
      text = File.read(file_name)
      File.open(file_name, "w") { |file| file << text.gsub("Framework", @appname) }
      flash[:notice] = "Enjoy your app."
      render('pages/home')
     end
    


    推荐答案

    您的路线文件没有名称的发布匹配,看起来像。

    Your routes file doesn't have a "post" match for namer, looks like. Easiest way to rectify that would be to put this line in somewhere.

    post '/namer' => 'namer#create'
    

    form_tag helper默认创建一个 post - 方法形式,如果你正在创建一个资源,那就是你想要的 - 你只需要确保你的路线在那里。有可能Rails不会通过底层的catch-all路由发送POST动作,但最好确保你的路由以某种方式命名。

    The form_tag helper defaults to creating a post-method form, and if you're creating a resource, that's what you'd want — you just need to make sure your route is there for it. It's possible that Rails doesn't send POST actions through the catch-all route at the bottom, but it's much better practice to ensure your routes are named somehow.

    就个人而言,只要可能,我更喜欢足智多谋的路线请在这里阅读;我承诺这是值得的。

    Personally, I prefer resourceful routes whenever possible. Read up on them here; I promise it's worth it.

    (如果这没有帮助...可以检查 def中的代码是否创建函数实际上正在触发?放在调试器行或放在 puts 语句中查找。)

    (If this doesn't help… can you check to see if the code in your def create function is actually firing? Drop in a debugger line or a puts statement to find out.)

    这篇关于提交表格(无模型)并且无法让控制器发挥其作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆