新的導軌,無法弄清楚如何做到這一點。我已經實現了使用設計的用戶登錄系統,我試圖讓用戶創建多個「列表」。有點像craigslist類型的網站。我可以從軌道控制檯填充數據庫,但我不知道如何把它放在網站上。將數據添加到嵌套的模型導軌4.0
我有以下型號:
listing.rb
class Listing < ActiveRecord::Base
belongs_to :user
default_scope -> { order('created_at DESC') }
#add validations
validates :user_id, presence: true
end
user.rb(用色器件)
class User < ActiveRecord::Base
has_many :listings, dependent: :destroy
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
我想創建一個網頁,其中允許用戶創建一個新的列表。我不確定如何去做這件事。這是我目前有:
listings_controller.rb
class ListingsController < ApplicationController
def index
@users = User.all
end
def show
@listing = Listing.find(params[:id])
end
def new
@listing = Listing.new
end
def create
@listing = Listing.new(listing_params)
if @listing.save
flash[:success] = "Success"
redirect_to @listing
else
render 'new'
end
end
private
def listing_params
params.require(:listing).permit(:id, :user_id, :title, :general_info)
end
end
模型/視圖/列表/ new.html.erb
<h1> POST A NEW LISTING </h>
<%= form_for @listing do |f| %>
Title: <%= f.text_field :title %> <br />
General Info: <%= f.text_field :general_info %> <br />
<%= f.submit %>
<% end %>
我一直都在做這個相當長一段時間沒有運氣得到數據庫來填充。目前,表單一旦提交,就會在def create中創建「else」並只呈現相同的頁面。
這裏是日誌輸出,當我運行此:
Started POST "/listings" for 127.0.0.1 at 2013-07-04 17:37:53 -0600
Processing by ListingsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mlyDb24OMQkniOCFQ1JTvzxjplHk7kMgzEBEFBH8hGw=", "listing"=>{"title"=>"title should go here", "general_info"=>"hope this works"}, "commit"=>"Create Listing"}
[1m[35m (0.1ms)[0m begin transaction
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
[1m[35mUser Load (0.3ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Rendered listings/new.html.erb within layouts/application (4.4ms)
Completed 200 OK in 17ms (Views: 10.4ms | ActiveRecord: 0.5ms)
我希望這是因爲'不是:tile'在'listing_params'方法。這是一個錯字還是你沒有注意到?你還看到你的日誌文件中有任何錯誤? – vee
好,我不知何故錯過了。然而,這不是問題,甚至沒有給出錯誤。我現在在OP中發佈了日誌輸出。 –
您使用的是devise 3.0.0.rc嗎? – deivid