Railsが初めてで、これを行う方法がわかりません。デバイスを使用してユーザーログインシステムを実装しましたが、ユーザーが複数の「リスト」を作成できるようにしようとしています。craigslist タイプのサイトのようなものです。Rails コンソールからデータベースにデータを入力することはできますが、それをサイトに配置する方法がわかりません。
私は次のモデルを持っています:
list.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)