私は Rails の初心者で、Michael の Hartl の railstutorial.org を読み終えたところです。私は今、最初のアプリを構築していて、最初の障害に遭遇しています。
つまり、基本的に、バブル (サブジェクトに相当) と噂があります。噂は泡に属し、泡には多くの噂があります。新しい噂を作成するとき、ユーザーはそれが属するバブルを選択する必要があります。
次のように2つのモデルを作成しました。
class Bubble < ActiveRecord::Base
attr_accessible :description, :name
has_many :rumors, dependent: destroy
validates :description, presence: true, length: { maximum: 300 }
validates :name, presence: true, length: { maximum: 50 }
end
と
class Rumor < ActiveRecord::Base
attr_accessible :content, :title, :bubble_id
belongs_to :bubble
validates :title, presence: true, length: { maximum: 30 }
validates :content, presence: true, length: { maximum: 300 }
validates :bubble_id, presence: true
end
新しい噂を作成するためのフォームは、ホームページに表示されます。
<%= simple_form_for(@rumor) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :content %>
<%= f.input :bubble_id, collection: @bubbles %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
したがって、StaticPagesController で変数を定義しました。
def home
@bubbles = Bubble.all
@rumor = Rumor.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @bubble }
end
end
私の Bubble コントローラは次のとおりです。 class BubblesController < ApplicationController
before_filter :authenticate_user!, only: [:create, :edit, :destroy]
# GET /bubbles
# GET /bubbles.json
def index
if params[:tag]
@bubbles = Bubble.tagged_with(params[:tag])
else
@bubbles = Bubble.all
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @bubbles }
end
end
# GET /bubbles/1
# GET /bubbles/1.json
def show
@bubble = Bubble.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @bubble }
end
end
# GET /bubbles/new
# GET /bubbles/new.json
def new
@bubble = Bubble.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @bubble }
end
end
# GET /bubbles/1/edit
def edit
@bubble = Bubble.find(params[:id])
end
# POST /bubbles
# POST /bubbles.json
def create
@bubble = Bubble.new(params[:bubble])
respond_to do |format|
if @bubble.save
format.html { redirect_to @bubble, notice: 'Bubble was successfully created.' }
format.json { render json: @bubble, status: :created, location: @bubble }
else
format.html { render action: "new" }
format.json { render json: @bubble.errors, status: :unprocessable_entity }
end
end
end
# PUT /bubbles/1
# PUT /bubbles/1.json
def update
@bubble = Bubble.find(params[:id])
respond_to do |format|
if @bubble.update_attributes(params[:bubble])
format.html { redirect_to @bubble, notice: 'Bubble was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @bubble.errors, status: :unprocessable_entity }
end
end
end
# DELETE /bubbles/1
# DELETE /bubbles/1.json
def destroy
@bubble = Bubble.find(params[:id])
@bubble.destroy
respond_to do |format|
format.html { redirect_to bubbles_url }
format.json { head :no_content }
end
end
end
噂のコントローラー:
class RumorsController < ApplicationController
before_filter :authenticate_user!, only: [:new, :create, :edit, :destroy]
# GET /rumors
# GET /rumors.json
def index
@rumors = Rumor.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @rumors }
end
end
# GET /rumors/1
# GET /rumors/1.json
def show
@rumor = Rumor.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @rumor }
end
end
# GET /rumors/new
# GET /rumors/new.json
def new
@rumor = Rumor.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @rumor }
end
end
# GET /rumors/1/edit
def edit
@rumor = Rumor.find(params[:id])
end
# POST /rumors
# POST /rumors.json
def create
@rumor = Rumor.new(params[:rumor])
respond_to do |format|
if @rumor.save
format.html { redirect_to @rumor, notice: 'Rumor was successfully created.' }
format.json { render json: @rumor, status: :created, location: @rumor }
else
format.html { render action: "new" }
format.json { render json: @rumor.errors, status: :unprocessable_entity }
end
end
end
# PUT /rumors/1
# PUT /rumors/1.json
def update
@rumor = Rumor.find(params[:id])
respond_to do |format|
if @rumor.update_attributes(params[:rumor])
format.html { redirect_to @rumor, notice: 'Rumor was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @rumor.errors, status: :unprocessable_entity }
end
end
end
# DELETE /rumors/1
# DELETE /rumors/1.json
def destroy
@rumor = Rumor.find(params[:id])
@rumor.destroy
respond_to do |format|
format.html { redirect_to rumors_url }
format.json { head :no_content }
end
end
end
フォームを介して関連付けを構築する方法を理解するのにまだかなり苦労しているので、問題の完全な解決策ではない場合でも、助けていただければ幸いです。