Ruby on rails 视图中的Rails多态帖子关联和表单_
在department视图中设置多态“department”帖子的表单时遇到了问题。我遵循rails cast教程了解多态关联 型号:Ruby on rails 视图中的Rails多态帖子关联和表单_,ruby-on-rails,forms,views,posts,polymorphism,Ruby On Rails,Forms,Views,Posts,Polymorphism,在department视图中设置多态“department”帖子的表单时遇到了问题。我遵循rails cast教程了解多态关联 型号: class Course < ActiveRecord::Base belongs_to :department, inverse_of: :courses has_and_belongs_to_many :users, -> { uniq } has_many :posts, as: :post
class Course < ActiveRecord::Base
belongs_to :department, inverse_of: :courses
has_and_belongs_to_many :users, -> { uniq }
has_many :posts, as: :postable #allows polymorphic posts
end
class Department < ActiveRecord::Base
has_many :courses, inverse_of: :department
has_many :posts, as: :postable #allows polymorphic posts
has_and_belongs_to_many :users, -> {uniq}
end
class Post < ActiveRecord::Base
belongs_to :user, touch: true #updates the updated_at timestamp whenever post is saved
belongs_to :postable, polymorphic: true #http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
belongs_to :department, counter_cache: true #for counting number of posts in department
belongs_to :course, counter_cache: true
validates :department_id, :course_id, presence: true
end
视图/部门/show.html.erb
<div class="tab-pane" id="posts"><br>
<center><h3>Posts:</h3></center>
<%= render "posts/form", postable: @department %>
</div>
错误为“未定义#的方法'posts_path'”
我认为这个错误与表单中的路径有关,但我不知道是什么。我也尝试过[@postable,@postable.posts.build]
,但这只给了我未定义的方法:PostsController
有人知道发生了什么以及我如何修复它吗?
@department
作为一个局部变量传递到表单partial中,但是表单调用一个实例变量:
# views/departments/show.html.erb
<%= render "posts/form", postable: @department %> # <------ postable
# views/posts/_form.html.erb
<%= form_for [@postable, Post.new] do |f| %> # <------ @postable
检查路线时,只能通过嵌套路线访问帖子<代码>发布路径不是有效的路由,其方法不存在,错误正确:未定义的方法“发布路径”
修正: 在departments controller中设置一个
@postable
实例变量,以便表单助手可以使用它:
def show
id = params[:id]
@postable, @department = Department.find(id) # <-- add @postable
@course = Course.new
@course.department_id = @department
end
def显示
id=params[:id]
@可邮寄,@department=department.find(id)#非常感谢,这确实帮助我清除了错误。但是,现在当我提交表单时,它会在PostsController中点击create方法,但是“if@post.save”条件不满足,因此它会呈现新操作和相应的视图。显然,我希望它保存,然后重定向到部门显示页面(部门路径)或部门帖子查看页面(部门帖子路径)。为什么不节约??再次感谢你的帮助!此验证将始终失败:验证:部门id、:课程id、状态:true
。它检查两个ID是否存在,这在多态关联的情况下是不可能的。你想要一个或另一个,但不是两个,或是异或。看这个问题:谢谢!这就解决了问题,我让这些职位都在工作。非常感谢。
class PostsController < ApplicationController
before_filter :find_postable
load_and_authorize_resource
def new
@postable = find_postable
@post = @postable.posts.new
end
def create
@postable = find_postable
@post = @postable.posts.build(post_params)
if @post.save
flash[:success] = "#{@post.title} was sucessfully created!"
redirect_to department_post_path#id: nil #redirects back to the current index action
else
render action: 'new'
end
end
def show
@post = Post.find(params[:id])
end
def index
@postable = find_postable
@posts = @postable.posts
end
...
private
def post_params
params.require(:post).permit(:title, :description, :content)
end
def find_postable #gets the type of post to create
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
def show
id = params[:id]
@department = Department.find(id)
@course = Course.new
@course.department_id = @department
end
# views/departments/show.html.erb
<%= render "posts/form", postable: @department %> # <------ postable
# views/posts/_form.html.erb
<%= form_for [@postable, Post.new] do |f| %> # <------ @postable
[@postable, Post.new] # => departments_posts_path
[ nil , Post.new] # => posts_path
def show
id = params[:id]
@postable, @department = Department.find(id) # <-- add @postable
@course = Course.new
@course.department_id = @department
end
<%= render "posts/form" %>