Rails: duplicating information into another model if conditions met -
i've got list of articles checkboxes on form. when box checked, body of selected article copied 1 of x text areas.
should user want make change body of article in text area need send change through controller new rails model (called edit).
i have record being created, need application record changes new edit record. here's relevant controller code:
new
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 |format| format.html format.json { render json: @doc } end end create
def create @doc = doc.new(params[:doc]) respond_to |format| if @doc.save #make editable version if current_user.brand.try(:editable?) @doc.articles.each |article| @edit = edit.new @edit.name = article.name @edit.video = article.video #something here bodies of 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 and here's code makes text areas in view.
<% @article_count.times |article| %> <div id="edit<%= article %>" class="tab-pane <%= "active" if article == 0 %>"> <%= text_area_tag("edit[#{article}][body]") %> </div> <% end %> records created each article, can't seem able save edits text areas. it's kind of nested-form arrangement. @ appreciated. cheers!
i've solved breaking form 2 pages: 1 handles selection, second editing. given doc has many edits, method made part 2 of form:
def edit_content @doc = doc.find(params[:id]) if !@doc.edits.exists? @doc.articles.reverse.each |article| @doc.edits.build(:body => article.body) end end end hope helps someone.
Comments
Post a Comment