Ruby on rails 标签和嵌套表单的表单\u-使用控制器中的自定义方法
我不熟悉Rails,也不熟悉应用程序,大学成员(教师和学生)可以在其中创建帖子并发表评论。稍后,我希望在其中添加嵌套(祖先)和积分系统 我有帖子,评论和会员模式。帖子模型是通过脚手架制作的,成员模型是通过设计制作的,评论只是一个模型 在我的帖子展示页面中,我希望在帖子下方有评论,我已经取得了一些进展(感谢SO,我了解了很多),但现在我遇到了一个问题,每当我尝试发布空白评论时,rails都会重定向到编辑页面。如何改变这一点,使rails只停留在显示页面上并显示错误 为此,我搜索了一下,在post_controller.rb中创建了一个新方法“update_comments”,并尝试修改标签属性的表单,如下面的代码所示,但现在我在提交时遇到路由错误 app/models/member.rbRuby on rails 标签和嵌套表单的表单\u-使用控制器中的自定义方法,ruby-on-rails,controller,show,nested-form-for,Ruby On Rails,Controller,Show,Nested Form For,我不熟悉Rails,也不熟悉应用程序,大学成员(教师和学生)可以在其中创建帖子并发表评论。稍后,我希望在其中添加嵌套(祖先)和积分系统 我有帖子,评论和会员模式。帖子模型是通过脚手架制作的,成员模型是通过设计制作的,评论只是一个模型 在我的帖子展示页面中,我希望在帖子下方有评论,我已经取得了一些进展(感谢SO,我了解了很多),但现在我遇到了一个问题,每当我尝试发布空白评论时,rails都会重定向到编辑页面。如何改变这一点,使rails只停留在显示页面上并显示错误 为此,我搜索了一下,在post_
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/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
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
您需要更新route中的方法类型,还需要将form post方法设置为新操作,并且在提交表单时也是一个post请求而不是get请求
Urdxxx::Application.routes.draw do
devise_for :members
resources :posts do
collection do
post :update_comment
end
end
root :to => 'posts#index'
有你的发言权 {:action=>'update_comment'}do | p |%> 4 %>
您不需要自定义方法。它不是很安静。例如,有关REST的信息,请参阅。在这种情况下,没有理由使用自定义方法 每当你发现自己在添加自定义方法时,你应该仔细考虑是否有必要。通常,如果您需要自定义方法,您实际需要的是另一个控制器(或一组不同的控制器) 这里的更新方法就是您所需要的。如果您真的想在更新失败后转到show方法(尽管我不知道为什么),那么在更新失败后更改更新方法中块中的
render edit
调用
看起来真正的问题是编辑视图没有显示错误。虽然scaffold生成的视图应该这样做,但是您可能已经更改了它
如果您错过了,您还可以从本屏幕广播中获益:
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
<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 %>
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
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 %>