如何在红宝石上制作非模型形式的铁轨? [英] How to do a non-model form in ruby on rails?

查看:42
本文介绍了如何在红宝石上制作非模型形式的铁轨?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Rails上制作红宝石的非模型表单,我发现的大多数示例都只有一个字段(例如搜索字段),或者使用了旧的方式来编写这样的表单使用Ruby on Rails的电子邮件表单

I'm trying to make a non-model form in ruby on rails, most of the examples I can find only have one field (say a search field) or use an old way of writing a form like this An Email Form with Ruby on Rails

如果有人可以向我展示非模型形式的示例代码,其中包含两个用于视图的字段以及如何访问控制器中的这些字段,我将不胜感激.

If anyone could show me example code of a non-model form with say two fields for the view and how I access those fields in the controller I'd be grateful.

非常感谢.

推荐答案

您将需要假设您要一个简单的test动作提交给do_test动作:

Say you want a simple test action that submits to do_test action:

测试操作的简单视图(posts/test.html.erb):

A simple view for test action (posts/test.html.erb):

<% form_tag '/posts/do_test' do %>
    <%=label_tag 'name' %>
    <%=text_field_tag 'name'%>

    <%=label_tag 'phone' %>
    <%=text_field_tag 'phone'%>

    <div><%= submit_tag 'Save' %></div>
<% end -%>

在posts控制器中:

In the posts controller:

def test
end

def do_test
  name = params[:name]
  phone = params[:phone]
  # do whatever you want...
end

还需要将这2个操作添加到config/routes.rb中的路由

Also you need to add these 2 actions to the routes in config/routes.rb

map.resources :posts, :collection=>{:test => :get, :do_test => :post}

这篇关于如何在红宝石上制作非模型形式的铁轨?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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