简体   繁体   English

根据lynda.com的截屏视频,Rails应该使用form_for(view)方法生成一个按钮,但事实并非如此。

[英]According to a lynda.com screencast, rails is supposed to be generating a button using the form_for (view) method, but it's not

EDIT: The first question is my premise itself. 编辑:第一个问题是我的前提本身。 Is rails/html SUPPOSED to generate a "Create Subject" button without me explicitly asking it to? 是否无需我明确要求rails / html即可生成“创建主题”按钮?

So here is the controller that is working with the view 所以这是使用视图的控制器

class SubjectsController < ApplicationController

  def index
     list
    render('list')
  end

  def list
    @subjects = Subject.order("subjects.position ASC")
  end

  def show
    @subject = Subject.find(params[:id])
  end

  def new
    @subject = Subject.new(:name => 'default')
  end
  def create
    #instantiate a new object using form params
    @subject = Subject.new(params[:subject])
    #save the subject
    if @subject.save
      #if save succeeds redirect to list action
    else
      #if save fails, redisplay form
      render('new')
    end
  end
end

And here is the misbehaving view (html.erb) file which isn't generating my button 这是行为不当的视图(html.erb)文件,它不会生成我的按钮

<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>

<div class="subject new">
  <h2>Create Subject</h2>

  <%=  form_for(:subject, :url => {:action => 'create'}) do |f| %>

  <table summary="Subject form fields">
    <tr>
      <th>Name</th>
      <td><%= f.text_field(:name) %></td>
    </tr>
    <tr>
      <th>Position</th>
      <td><%= f.text_field(:position) %></td>
    </tr>
    <tr>
      <th>Visible</th>
      <td><%= f.text_field(:visible) %></td>
    </tr>
  </table>
<% end %>
</div>

Currently, the output on the browser is: 当前,浏览器上的输出为:

'<< Back to List' (link)

<h2>Create Subject</h2>

Name     [blank-form]
Position [blank-form]
Visible  [blank-form]
[missing button location]

There is supposed to (according to lynda.com) be a button which says "Create Subject" in the missing button location, but it's not there. (根据lynda.com)应该有一个在丢失的按钮位置显示“创建主题”的按钮,但是它不存在。

Nothing in your code is supposed to generate a button. 您的代码中没有任何内容应该生成按钮。

You'll need to add: 您需要添加:

<%= f.submit 'Create Subject' %>

inside the form. 在表格内。 Maybe between </table> and <% end %> 也许在</table><% end %>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM