mercredi 11 mars 2015

GET errors in ruby on rails. Controllers and actions. CRUD

I'm working through the "Ruby on rails essential 3 training" on lynda.com. I'm at the point of creating forms and implementing CRUD. So far whenever I try to have rails initiate an action, it can never find the definition. To solve this problem I define the action within the controller, and then create a GET, within my routes.rb file. My instructor doesn't have to do this and his server starts just fine. I have a feeling I'm doing something wrong because my routes.rb file has far to many GET commands. Please look at the following folders and let me know if you have an idea of why I have to make a GET every time I try a new action. I heard that when creating a Model, there is supposed to be a GET created at the same time but that isn't happening and I'm not sure if I'm understanding that correctly.


My subjects controller:



class SubjectsController < ApplicationController

def index
list
render('list')
end

# def index
# show
# render('show')
# end

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

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

def new
@subject = Subject.new

end

def create

end


end


My new.html.erb file:



<%= 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>

<div class="form-buttons">
<%= submit_tag("Create Subject") %>
</div>
<% end %>


My config/routes.rb



Rails.application.routes.draw do
root :to=>"demo#index"
get 'demo/index'
get 'demo/hello'
get 'demo/other_hello'
get 'subjects/list'
get 'subjects/show'
get 'subjects/new'
get 'subjects/create'

Aucun commentaire:

Enregistrer un commentaire