0

所以我最初的问题是Association 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
4

1 回答 1

0

我对 Rails 还很陌生,但我认为这就是你的做法。我假设您当前登录的用户是current_user.

def create
    @post = current_user.posts.build(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
于 2012-07-20T14:29:45.160 回答