Reputation: 2665
I'm working on a webapp that allows users to create scavenger hunts. Trouble is, I can't seem to convince Rails to save new hunts. Here's my setup. Any idea what I'm doing wrong?
Model:
class Hunt < ActiveRecord::Base
attr_accessible :name
validates :name, :presence => true,
:length => { :maximum => 50 } ,
:uniqueness => { :case_sensitive => false }
end
Controller:
class HuntController < ApplicationController
def index
@title = "All Hunts"
@hunts = Hunt.order("name ASC")
end
def show
@hunt = Hunt.find(params[:id])
@title = @hunt.name
end
def new
@hunt = Hunt.new
@title = "New Hunt"
end
def create
@hunt = Hunt.new(params[:hunt]) #fixed type (used to be this: Hunt.new(params[:id]) )
if @hunt.save
flash[:success] = "Hunt created!"
redirect_to index
else
@title = "New Hunt"
render 'new'
end
...
end
View
<h1>Sign up</h1>
<%= form_for(@hunt) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
Routes
MyChi::Application.routes.draw do
get "hunts/index"
get "hunts/new"
get "hunts/create"
get "hunts/show"
get "hunts/list"
get "hunts/edit"
get "hunts/delete"
match '/hunts', :to => 'hunts#index'
resource :hunts
.....
root :to => "pages#home"
match ':controller(/:action(/:id(.:format)))'
EDIT: I should mention that when a new hunt is added, the app redirects the user to Index, but there's no flash notification of success.
Upvotes: 0
Views: 305
Reputation: 4097
In your create action, change Hunt.new(params[:id]) to Hunt.new(params[:hunt])
Upvotes: 2