所以我最初的問題是Association Ruby on Rails。然而,當提到一個帖子時,我所遇到的問題就是它的空白。沒有看到任何東西。經過研究,我相信是因爲當我創建我的測試帖時,我沒有定義與其相關的客戶。Ruby on Rails修改參考
class CustomersController < ApplicationController
before_filter :signed_in_customer, only: [:edit, :update]
before_filter :correct_customer, only: [:edit, :update]
def index
@customers = Customer.all
end
def show
@customer = Customer.find(params[:id])
**@posts = @customer.posts**
end
這裏的表單允許我創建一個帖子的時刻。
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :frienship_group_id %><br />
<%= f.text_field :frienship_group_id %>
</div>
<div class="field">
<%= f.label :post_size_id %><br />
<%= f.text_field :post_size_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
這是我在創建新表單時使用的。我如何讓自定義更新與customer_id字段中的當前用戶相關的字段?
這裏模型
class Post < ActiveRecord::Base
belongs_to :customer
validates :title, presence: true
validates :description, presence: true
validates :customer_id, presence: true
validates :frienship_group_id, presence: true
validates :Post_size_id, presence: true
更新:
這裏我new.html.erb爲崗位
<h1>New post</h1>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>
這裏控制器
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
# GET /posts/1
# GET /posts/1.json
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
# GET /posts/new
# GET /posts/new.json
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.json
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
end
你需要在這裏包括你的動作'創造'的職位。 – gabrielhilal 2012-07-20 14:15:09
我包含後 – Jseb 2012-07-20 14:24:46