私はRailsが新しく、大学のメンバー(教師と学生)が投稿を作成してコメントを作成できるアプリケーションです。後で、ネスティング(祖先)とポイントシステムを追加したいと考えています。form_forタグとネストされたフォーム - コントローラのカスタムメソッドを使用する
投稿、コメント、メンバーのモデルがあります。 PostモデルはScaffoldingを介して作成され、MemberモデルはDeviseの助けを借りて作成され、Commentは単なるモデルに過ぎません。
ポストの私のショーページでは、ポストの下にコメントがありますが、私はいくつかの進歩を遂げました。(だからおかげさまで私はかなり知りました)しかし今はいつも問題に悩まされています空白のコメントを投稿しようとしましたが、railsは編集ページにリダイレクトしていました。レールを表示ページにだけ残してエラーを表示するようにこれを変更するには?
私は少し検索し、post_controller.rbの新しいメソッド 'update_comments'を作成し、以下のコードのようにforms_forタグ属性を変更しようとしましたが、送信時にルーティングエラーが発生します。
APP /モデル/ 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 /モデル/ post.rb
class Post < ActiveRecord::Base
#Associations
belongs_to :member
has_many :comments, :dependent => :destroy
accepts_nested_attributes_for :comments
end
APP /モデル/ comment.rb
class Comment < ActiveRecord::Base
# Associations
belongs_to :member
belongs_to :post
validates_presence_of :content
end
設定/ルート。 rb
Urdxxx::Application.routes.draw do
devise_for :members
resources :posts do
member do
get 'update_comment'
end
end
root :to => 'posts#index'
作る上
http://i.stack.imgur.com/TBgKy.png
:
アプリ/コントローラ/ 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
アプリ/ビュー/記事/ 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/JlWeR.png
更新:
ケンは次のように振り返って変更を加えました。私は方法はわかりませんが、今のところうまくいきます。あなたがそのポストフォームを送信するときは、ルート内のメソッドタイプを更新する必要があるともに必要
アプリ/コントローラ/ 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