简体   繁体   English

Rails表单未从另一个控制器呈现

[英]Rails form is not getting rendered from another controller

I am making a simple todo list application where I have this below code in my todos/new template 我正在制作一个简单的待办事项列表应用程序,在我的待办事项/新模板中有以下代码

todos/new.html.erb

<div class="new-task">
    <%= form_tag {controller: "todos", action: "new"} , method: "post", class: "form-group" do %>
        <div class="row">
            <div class="col-md-8 col-sm-8">
                <%= text_field_tag "todos[task]", "", class: "form-control" %>
            </div>
            <div class="col-md-4 col-sm-4">
                <%= submit_tag "Add", class: "btn btn-primary" %>
            </div>
        </div>
    <% end %>
</div>

When I am trying to render this view from another controller's view using 当我尝试使用另一个控制器的视图渲染此视图时

<%= render template: 'todos/new' %>

I am getting a nasty error with below stack trace. 我在下面的堆栈跟踪中遇到了一个令人讨厌的错误。

SyntaxError in ToolsController#index

app/views/todos/new.html.erb:2: syntax error, unexpected ':', expecting '}'
app/views/todos/new.html.erb:2: syntax error, unexpected ',', expecting '}'
app/views/todos/new.html.erb:2: syntax error, unexpected tLABEL
app/views/todos/new.html.erb:2: syntax error, unexpected ',', expecting keyword_end
app/views/todos/new.html.erb:2: syntax error, unexpected keyword_do, expecting keyword_end
app/views/todos/new.html.erb:19: syntax error, unexpected keyword_ensure, expecting end-of-input
app/views/tools/index.html.erb:18:in `_app_views_tools_index_html_erb__1039417368_95034624'

Please help in resolving this. 请帮助解决此问题。 Thanks in advance! 提前致谢!

form_tag(url_for_options = {}, options = {}, &block) form_tag(url_for_options = {},options = {},&block)

The form_tag helper accepts 2 arguments: the path for the action and an options hash. form_tag帮助程序接受2个参数:操作的路径和选项哈希。 This hash specifies the method of form submission and HTML options such as the form element's class 该哈希指定表单提交的方法和HTML选项,例如表单元素的类

In your case you need to wrap the controller and action between curly braces, and the rest of options within parenthesis, like: 在您的情况下,您需要将控制器和操作包装在花括号之间,并在括号内包含其他选项,例如:

<%= form_tag({controller: 'todos', action: 'new'}, class: 'form-group') do |form| %>
  ...

This way Rails will turn the hash into a valid URL. 这样,Rails会将哈希值转换为有效的URL。

Same could be achieved by passing the URI corresponding to both, controller and action, like: 通过传递与控制器和操作相对应的URI可以实现相同的目的,例如:

<%= form_tag('/todos', class: 'form-group') do |form| %>
  ...

Note if you leave a white space between the form_tag and the parenthesis you will have a syntax error, unexpected tLABEL append= form_tag error and the default method for the form_tag helper is POST, so you don't need to specify it in this case. 请注意,如果在form_tag和括号之间留有空白,则会出现syntax error, unexpected tLABEL append= form_tag错误,并且form_tag帮助程序的默认方法是POST,因此在这种情况下无需指定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails在控制器动作中形成,在另一个视图中呈现? - Rails form in a controller action, rendered in another view? 从Ruby on Rails 3中的另一个控制器渲染表单 - Rendering a form from another controller in Ruby on Rails 3 Rails-将值从视图表单中获取到控制器中 - Rails - getting the value out from the view form into a controller 用来自另一个控制器的模型信息填充Rails表单 - Populate Rails Form With Model Information From Another Controller Rails 4如何从另一个控制器中获取ID - Rails 4 How to get id from another controller in form 从另一个控制器Rails 4.0进行错误验证后,重新填充表单 - Repopulate form after error validation from another controller Rails 4.0 Rails从渲染的表单中删除参数 - Rails remove params from rendered form 在Ruby on Rails中,我可以使用新的控制器来显示从另一个控制器上的表单获取的列信息吗? - Within Ruby on Rails, can I use a new controller to display column information obtained from a form on another controller? Ruby on Rails:如何通过表单将实例变量从控制器传递到另一个控制器 - Ruby on Rails: how to pass an instance variable from a controller to another controller via a form 在轨道上的ruby中将表单值转换为控制器 - getting form value into controller in ruby on rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM