Ruby on rails 3 编辑(Rails)时字段的值是否消失?
这是处理两个模型的形式:Post和Tag都有一个Ruby on rails 3 编辑(Rails)时字段的值是否消失?,ruby-on-rails-3,Ruby On Rails 3,这是处理两个模型的形式:Post和Tag都有一个has\u和\u属于\u多个相互关联。标记模型有一个名为:name的属性,Post模型有一个名为:Tag\u name的属性访问器(以便自动完成): 使用rails3 jquery自动完成gem的此字段存储我输入的值。但当我单击“编辑”时,字段为空,但奇怪的是,我可以输入更多文本并单击“更新”: <%= f.autocomplete_field :tag_name, autocomplete_tag_name_posts_path, :"da
has\u和\u属于\u多个相互关联。标记模型有一个名为:name
的属性,Post模型有一个名为:Tag\u name
的属性访问器(以便自动完成):
使用rails3 jquery自动完成gem的此字段存储我输入的值。但当我单击“编辑”时,字段为空,但奇怪的是,我可以输入更多文本并单击“更新”:
<%= f.autocomplete_field :tag_name, autocomplete_tag_name_posts_path, :"data-delimiter" => ' ' %>
“”%>
有什么建议可以解决这个问题吗?恐怕我不熟悉自动完成gem,但是您是否尝试过手动设置自动完成\u字段中的值?
class Post < ActiveRecord::Base
attr_accessible :title, :content, :tag_name
attr_accessor :tag_name
def tag!(tags)
tags = tags.split(" ").map do |tag|
Tag.find_or_create_by_name(tag)
end
self.tags << tags
end
end
def create
@user = current_user
@post = @user.posts.new(params[:post])
if @post.save
redirect_to @post, notice: 'post was successfully created.'
else
render action: "new"
end
@post.tag!(params[:post][:tag_name])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
@post.update_attributes(params[:post])
@post.tag!(params[:post][:tag_name])
redirect_to @post
end
<%= f.autocomplete_field :tag_name, autocomplete_tag_name_posts_path, :"data-delimiter" => ' ' %>