Method: WikiController#update

Defined in:
app/controllers/wiki_controller.rb

#updateObject

Creates a new page or updates an existing one



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/controllers/wiki_controller.rb', line 155

def update
  @page = @wiki.find_or_new_page(params[:id])
  return render_403 unless editable?

  was_new_page = @page.new_record?
  @page.safe_attributes = params[:wiki_page]

  @content = @page.content || WikiContent.new(:page => @page)
  content_params = params[:content]
  if content_params.nil? && params[:wiki_page].present?
    content_params = params[:wiki_page].slice(:text, :comments, :version)
  end
  content_params ||= {}

  @content.comments = content_params[:comments]
  @text = content_params[:text]
  if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
    @section = params[:section].to_i
    @section_hash = params[:section_hash]
    @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(@section, @text, @section_hash)
  else
    @content.version = content_params[:version] if content_params[:version]
    @content.text = @text
  end
  @content.author = User.current

  if @page.save_with_content(@content)
    attachments = Attachment.attach_files(@page, params[:attachments] || (params[:wiki_page] && params[:wiki_page][:uploads]))
    render_attachment_warning_if_needed(@page)
    call_hook(:controller_wiki_edit_after_save, {:params => params, :page => @page})

    respond_to do |format|
      format.html do
        anchor = @section ? "section-#{@section}" : nil
        redirect_to project_wiki_page_path(@project, @page.title, :anchor => anchor)
      end
      format.api do
        if was_new_page
          render :action => 'show', :status => :created, :location => project_wiki_page_path(@project, @page.title)
        else
          render_api_ok
        end
      end
    end
  else
    respond_to do |format|
      format.html {render :action => 'edit'}
      format.api {render_validation_errors(@content)}
    end
  end

rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError
  # Optimistic locking exception
  respond_to do |format|
    format.html do
      flash.now[:error] = l(:notice_locking_conflict)
      render :action => 'edit'
    end
    format.api {render_api_head :conflict}
  end
end