无法保存新的Rails 3 ActiveRecord模型

时间:2012-03-24 20:51:51

标签: ruby-on-rails rails-activerecord

我正在开发一个允许用户创建清道夫狩猎的网络应用程序。麻烦的是,我似乎无法说服Rails拯救新的狩猎。这是我的设置。知道我做错了吗?

型号:

class Hunt < ActiveRecord::Base
  attr_accessible :name

  validates :name,  :presence => true,
                    :length   => { :maximum => 50 } ,
                    :uniqueness => { :case_sensitive => false }
end

控制器:

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

查看

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

路线

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)))'

编辑:我应该提一下,当添加新的搜索时,应用会将用户重定向到索引,但没有成功的闪光通知。

1 个答案:

答案 0 :(得分:2)

在你的创建动作中,将Hunt.new(params [:id])更改为Hunt.new(params [:hunt])