创建记录时出现问题(CRUD)

时间:2011-08-19 10:10:48

标签: ruby-on-rails

我尝试使用Rail 3在我的数据库中保存数据,但我无法做到这一点 发生错误,如下所示: Vorlesungs中的NoMethodError#new

在我的控制器中:

class VorlesungsController < ApplicationController
  def new
         @vorlesung=Vorlesung.new
  end

 def create
    @vorlesung=Vorlesung.create(params[:vorlesung])
    if @vorlesung.save
       @status_message = 'Student inserted successfully.'
    else
      render 'new'
    end
  end
end

和我的观点:

    <%= form_for @vorlesung do |v| %>
    Name : <%= v.text_field :Name %>    <br>
    Name de Professur : <%= v.text_field :Leiter_name %><br>
    <%= v.submit 'Speicher'%>

<% end %>

当我将Form_for更改为   &lt;%= form_for @vorlesung do | v | %GT; 发生这样的错误:    Vorlesungs中的NoMethodError #new 当Form_for保持为:   &lt;%= form_for:vorlesung do | v | %GT; 单击我的提交按钮后,只删除了文本框的内容 没有其他影响。非常感谢你的帮助

这是我的完整错误消息:

NoMethodError in Vorlesungs#new

Showing /home/babak/Management/app/views/vorlesungs/new.erb where line #1 raised:

undefined method `vorlesungs_path' for #<#<Class:0xb5f55cc0>:0xb5f54f00>
Extracted source (around line #1):

1: <%= form_for @vorlesung do |v| %>
2:     Name : <%= v.text_field :Name %>    <br>
3:     Name de Professur : <%= v.text_field :Leiter_name %><br>
4:     <%= v.submit 'Speicher'%>

这是我的路线档案:

Management::Application.routes.draw do
 # get "vorlesungs/Show"
  root :to => 'vorlesungs#Show'
  match 'vorlesungs/new' =>'vorlesungs#new'
end

1 个答案:

答案 0 :(得分:0)

您尚未在路线中定义创建操作。

而不是

 # get "vorlesungs/Show"
  root :to => 'vorlesungs#Show'
  match 'vorlesungs/new' =>'vorlesungs#new'

添加

resources :vorlesungs

可以找到一个很好的指南here

相关问题