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