开发者

RoR Error: undefined method students_index_path

I have been getting this error and have been unable to fix it

(the post at StackOverflow Undefined Method Index Path didn't fix my problem).

This exact error message I am getting is (I am using Rails 3.0.5 and Ruby 1.9.2):


NoMethodError in Students#new  

Showing C:/rails/ww/app/views/students/_form.html.erb where line #1 raised:

undefined method `students_index_path' for #<#:0x4991c10>

Here are the files:

students_controller.rb


    class StudentsController < ApplicationController

      def new
         @students = Students.new
      end
    end

new.html.erb


<h1>Enroll New Student</h1>

<%= render 'form' %>

<%= link_to 'Back', students_path %>

_form.html.erb


<%= form_for(@students) do |f| %>
  <% if @students .errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@students .errors.count, "error") %> prohibited this course from being saved:</h2>

      <ul>
      <% @students .errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :pen %><br />
    <%= f.text_field :pen %>
  </div>
  <div class="field">
    <%= f.label :fName %><br />
    <%= f.text_field :fName %>
  </div>
  <div class="field">
    <%= f.label :lName %><br />
    <%= f.text_field :lName %>
  </div>
  <div class="field">
    <%= f.label :pass %><br />
 开发者_如何学Python   <%= f.text_field :pass %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Thanks for the answer in advanced.


It should be <%= form_for(@student) do |f| %> not @students


Why your model name is plural ? It should be singular.
Just rename or drop/create your model with name Student.

Add resources :students to config/routes.rb

In controller, @student = Student.new

Thats it.. should work with this...


This is because you are passing in the variable @students into form_for, so rails interprets that as students_index_path. If you were to pass in a variable named @student, you'd fine. (assuming you created a variable @student = Student.new)

I'd read up on how form_for interprets it's arguments here.


Just add string in routes.rb

resources :students

instead

match "/students/:id" => "students#new"

or whatever you have (:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜