Ruby on rails Rails:如果条件满足,则将信息复制到另一个模型中
我有一张表格上有复选框的文章列表。选中框后,所选文章的正文将复制到Ruby on rails Rails:如果条件满足,则将信息复制到另一个模型中,ruby-on-rails,ruby-on-rails-3,model-view-controller,nested-forms,Ruby On Rails,Ruby On Rails 3,Model View Controller,Nested Forms,我有一张表格上有复选框的文章列表。选中框后,所选文章的正文将复制到x文本区域之一 如果用户希望对文本区域中的文章正文进行更改,我需要通过控制器将更改发送到新的Rails模型中(称为编辑) 我已经创建了记录,我只需要应用程序将更改记录到新的Edit记录中。以下是我的相关控制器代码: 新的 def new @template = Template.find(params[:template]) @article_count = @template.pages-1 @doc = Doc.n
x
文本区域之一
如果用户希望对文本区域中的文章正文进行更改,我需要通过控制器将更改发送到新的Rails模型中(称为编辑
)
我已经创建了记录,我只需要应用程序将更改记录到新的Edit
记录中。以下是我的相关控制器代码:
新的
def new
@template = Template.find(params[:template])
@article_count = @template.pages-1
@doc = Doc.new
@doc.template_id = @template.id
@doc.user_id = current_user.id
@articles = current_user.brand.articles
respond_to do |format|
format.html
format.json { render json: @doc }
end
end
创建
def create
@doc = Doc.new(params[:doc])
respond_to do |format|
if @doc.save
#make editable version
if current_user.brand.try(:editable?)
@doc.articles.each do |article|
@edit = Edit.new
@edit.name = article.name
@edit.video = article.video
#something here to get the bodies of the text areas
@edit.article_id = article.id
@edit.doc_id = @doc.id
@edit.user_id = current_user.id
@edit.save
end
end
@doc.create_activity :create, owner: current_user
format.html { redirect_to share_url(@doc.user.ftp, @doc) }
format.json { render json: @doc, status: :created, location: @doc }
else
format.html { render action: "new" }
format.json { render json: @doc.errors, status: :unprocessable_entity }
end
end
end
下面是在视图中创建文本区域的代码
<% @article_count.times do |article| %>
<div id="edit<%= article %>" class="tab-pane <%= "active" if article == 0 %>">
<%= text_area_tag("edit[#{article}][body]") %>
</div>
<% end %>
为每篇文章创建记录,但我似乎无法保存文本区域的编辑。这是一种嵌套形式的排列。任何帮助都将不胜感激。干杯 我将表单分为两页来解决这个问题:一页处理选择,另一页处理编辑。考虑到
文档
有许多编辑
,这是我为表单第二部分所做的方法:
def edit_content
@doc = Doc.find(params[:id])
if !@doc.edits.exists?
@doc.articles.reverse.each do |article|
@doc.edits.build(:body => article.body)
end
end
end
希望这能帮助别人