0

Railsは初めてで、大学のメンバー(教師と学生)が投稿を作成してコメントできるアプリケーションを作成しています。後で、ネスト(祖先)とポイントシステムを追加したいと思います。

投稿、コメント、メンバーモデルがあります。PostモデルはScaffoldingを介して作成され、MemberモデルはDeviseの助けを借りて作成され、Commentは単なるモデルです。

投稿のショーページで、投稿の下にコメントを入れたいのですが、ある程度の進歩がありました(SOのおかげで、かなりのことを知るようになりました)が、投稿しようとすると問題が発生します。空白のコメント、railsは編集ページにリダイレクトしていました。レールが表示ページと表示エラーのみにとどまるようにこれを変更するにはどうすればよいですか?

このために少し検索し、post_controller.rbに新しいメソッド「update_comments」を作成し、以下のコードのようにforms_forタグ属性を変更しようとしましたが、送信時にルーティングエラーが発生します。

app / models / member.rb

class Member < ActiveRecord::Base
  #Associations
  belongs_to :department

  has_one :student, :dependent => :destroy
  accepts_nested_attributes_for :student

  has_one :nstudent, :dependent => :destroy
  accepts_nested_attributes_for :nstudent

  has_many :posts, :dependent => :destroy
  has_many :comments, :dependent => :destroy  
end

app / models / post.rb

class Post < ActiveRecord::Base

  #Associations
  belongs_to :member
  has_many :comments, :dependent => :destroy

  accepts_nested_attributes_for :comments
end

app / models / comment.rb

class Comment < ActiveRecord::Base

  # Associations
  belongs_to :member
  belongs_to :post

  validates_presence_of :content
end

config / routers.rb

Urdxxx::Application.routes.draw do
  devise_for :members

  resources :posts do
    member do
     get 'update_comment'
    end
  end

  root :to => 'posts#index'

app / controllers / posts_controller.rb

class PostsController < ApplicationController
  # Devise filter that checks for an authenticated member
  before_filter :authenticate_member!

# GET /posts
# GET /posts.json
def index
  @posts = Post.find(:all, :order => 'points DESC')

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @posts }
  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])
  @post.member_id = current_member.id if @post.member_id.nil?

  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

# Not made by scaffold
def update_comment
  @post = Post.find(params[:id])        

  respond_to do |format|
    if @post.update_attributes(params[:post])
      format.html { redirect_to @post, notice: 'Comment was successfully created.' }
      format.json { head :no_content }
    else
      format.html { render action: "show" }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end
end

app / views / posts / show.html.erb

<p> Have your say </p>
<%= form_for @post, :url => {:action => 'update_comment'} do |p| %>
  <%= p.fields_for :comments do |c| %>
     <!-- Following 3 lines saved my life -->
      <% if c.object.new_record? %>
        <%= c.text_area :content, :rows => 4 %>
        <%= c.hidden_field :member_id, value: current_member.id %>
      <% end %>
   <% end %>
   <%= p.submit "Reply" %>
<% end %>

私のショーページの画像:http: //i.stack.imgur.com/TBgKy.png

コメントの作成について:http: //i.stack.imgur.com/JlWeR.png

アップデート:

ケンが言ったことに従って、ここで振り返って変更を加えました。方法はわかりませんが、今のところは機能します。

app / controllers / posts_controller.rb

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 }
  elsif :comments
    format.html { render action: "show" }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  else
    format.html { render action: "edit" }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
  end
end
4

2 に答える 2

0

ルート内のメソッドタイプを更新する必要があります。また、フォームを送信するときに、getリクエストではなくpostリクエストとして、フォームのpostメソッドを新しいアクションに設定する必要があります。

Urdxxx::Application.routes.draw do
  devise_for :members

  resources :posts do
    collection do
     post :update_comment
    end
  end

  root :to => 'posts#index'

<p> Have your say </p>
<%= form_for :post, :url => {:action => 'update_comment'} do |p| %>
  <%= p.fields_for :comments do |c| %>
     <!-- Following 3 lines saved my life -->
      <% if c.object.new_record? %>
        <%= c.text_area :content, :rows => 4 %>
        <%= c.hidden_field :member_id, value: current_member.id %>
      <% end %>
   <% end %>
   <%= p.submit "Reply" %>
<% end %>
于 2012-03-26T06:19:19.280 に答える
0

カスタムメソッドは必要ありません。それはあまりRESTfulではありません。RESTの詳細については、たとえばhttp://www.sitepoint.com/restful-rails-part-i/を参照してください。これは、カスタムメソッドを使用する正当な理由がある場合ではありません。

カスタムメソッドを追加していることに気付いたときはいつでも、それが必要かどうかについてじっくり考えるべきです。通常、カスタムメソッドが必要な場合、実際に必要なのは別のコントローラー(または別のコントローラーのセット)です。

ここでの更新方法はあなたが必要とするすべてです。更新が失敗した後に本当にshowメソッドに移動したい場合は(理由はわかりませんが)render edit、更新が失敗した後、updateメソッドのブロック内の呼び出しを変更します。

あなたの本当の問題は、編集ビューにエラーが表示されていないことのようです。スキャフォールドで生成されたビューはそれを行う必要がありますが、おそらくそれを変更しました。

見逃した場合は、このスクリーンキャストの恩恵を受けることもできます。

http://railscasts.com/episodes/196-nested-model-form-part-1

于 2012-03-26T06:27:08.683 に答える