Preserve message details over validation errors

If a validation error occurs while saving a message then make
sure the values are preserved in the new form.
This commit is contained in:
Tom Hughes 2014-02-04 22:31:44 +00:00
parent a52242ccbc
commit 25b93471c1
3 changed files with 50 additions and 7 deletions

View file

@ -29,6 +29,7 @@ class MessageController < ApplicationController
end
end
else
@message = Message.new(:recipient => @this_user)
@title = t 'message.new.title'
end
end
@ -40,9 +41,13 @@ class MessageController < ApplicationController
if message.to_user_id == @user.id then
message.update_attribute(:message_read, true)
@body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
@title = @subject = "Re: #{message.title.sub(/^Re:\s*/, '')}"
@this_user = User.find(message.from_user_id)
@message = Message.new(
:recipient => message.sender,
:title => "Re: #{message.title.sub(/^Re:\s*/, '')}",
:body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}",
)
@title = @message.title
render :action => 'new'
else

View file

@ -1,18 +1,18 @@
<% content_for :heading do %>
<h2><%= raw(t'message.new.send_message_to', :name => link_to(h(@this_user.display_name), {:controller => 'user', :action => 'view', :display_name => @this_user.display_name})) %></h2>
<h2><%= raw(t'message.new.send_message_to', :name => link_to(h(@message.recipient.display_name), {:controller => 'user', :action => 'view', :display_name => @message.recipient.display_name})) %></h2>
<% end %>
<%= error_messages_for 'message' %>
<%= form_for :message, :html => { :class => 'standard-form' }, :url => { :action => "new", :display_name => @this_user.display_name } do |f| %>
<%= form_for :message, :html => { :class => 'standard-form' }, :url => { :action => "new", :display_name => @message.recipient.display_name } do |f| %>
<fieldset>
<div class='form-row'>
<label class="standard-label"><%= t'message.new.subject' %></label>
<%= f.text_field :title, :size => 60, :value => @subject, :class => "richtext_title" %>
<%= f.text_field :title, :size => 60, :class => "richtext_title" %>
</div>
<div class='form-row'>
<label class="standard-label"><%= t'message.new.body' %></label>
<%= richtext_area :message, :body, :cols => 80, :rows => 20, :value => @body %>
<%= richtext_area :message, :body, :cols => 80, :rows => 20 %>
</div>
<div class='buttons'>
<%= submit_tag t('message.new.send_button') %>