Reputation: 1951
im a rails (and ruby) newbie and struggling with an a scaffolded controller that i was trying to tweak more to my configurations- the record is being saved but the fields in the db are null- here is the result of the development.log
Started POST "/events" for 127.0.0.1 at 2011-12-12 10:36:55 +0000
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mizap5163W5RbRrHiWdepYZDgrsaflhJPWaVhdHlaOA=", "event"=>{"title"=>"bla de bla", "details"=>"bla de bla", "ticket_info"=>"bla de bla", "when(1i)"=>"2011", "when(2i)"=>"12", "when(3i)"=>"12", "when(4i)"=>"10", "when(5i)"=>"34", "ends(1i)"=>"2011", "ends(2i)"=>"12", "ends(3i)"=>"12", "ends(4i)"=>"10", "ends(5i)"=>"34"}, "commit"=>"Create Event"}
[1m[35m (0.0ms)[0m BEGIN
[1m[36mSQL (0.0ms)[0m [1mINSERT INTO `events` (`created_at`, `details`, `ends`, `image_url`, `location`, `organiser_id`, `ticket_info`, `title`, `updated_at`, `when`) VALUES ('2011-12-12 10:36:55', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2011-12-12 10:36:55', NULL)[0m
[1m[35m (45.0ms)[0m COMMIT
Redirected to http://localhost:3000/events/11
Completed 302 Found in 67ms
the form and the controller are pretty basic so i do not know where i have gone wrong.
controller methods-
# GET /Events/new
# GET /Events/new.json
def new
@Event = Event.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @Event }
end
end
# GET /Events/1/edit
def edit
@Event = Event.find(params[:id])
end
# POST /Events
# POST /Events.json
def create
@Event = Event.new(params[:Event])
respond_to do |format|
if @Event.save
format.html { redirect_to @Event, notice: 'Event was successfully created.' }
format.json { render json: @Event, status: :created, location: @Event }
else
format.html { render action: "new" }
format.json { render json: @Event.errors, status: :unprocessable_entity }
end
end
end
And the form (probably very basic mistakes)
<%= form_for(@Event) do |f| %>
<% if @Event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@Event.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% @Event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :details %><br />
<%= f.text_area :details %>
</div>
<div class="field">
<%= f.label :ticket_info %><br />
<%= f.text_area :ticket_info %>
</div>
<div class="field">
<%= f.label :when %><br />
<%= f.datetime_select :when %>
</div>
<div class="field">
<%= f.label :ends %><br />
<%= f.datetime_select :ends %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and the model- (i added in the attr_accessible tags thinking it owuld make a difference)
class Event < ActiveRecord::Base
attr_accessible :title, :Details, :image_url, :organiser_id, :ticket_info, :when, :ends, :location
end
you can see from the log that the sql query is not inserting a row with any data but the params are they
Upvotes: 1
Views: 1516
Reputation: 50057
Check out the ruby naming conventions here.
It is advised to always use lowercase instance variables! So use @event
instead of @Event
.
So following rails guidelines (and your scaffold would have looked the same), your controller create
method should look like:
def create
@event = Event.new(params[:event])
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render json: @event, status: :created, location: @event }
else
format.html { render action: "new" }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
Note that only class-names, module-names and constants are ever capitalized. Symbols are always lower-cased by convention.
Hope this helps.
Upvotes: 0
Reputation: 8954
This is wrong:
def create
@Event = Event.new(params[:Event])
....
end
It should be:
def create
@Event = Event.new(params[:event])
....
end
hf...
// And also you should use downcase in cvariable Names. Uppercase should only be used for Constants like the ClassName!
Upvotes: 6