0

I want to preview my form content before save, when user click button to preview. Here, below is my create action

def create
     @article = Article.new(params[:article])
      if params[:preview_button] || !@article.save
        render :action => 'new'
      else
        @article.save
        flash[:notice] = "Successfully created Article."
        redirect_to article_path(@article)
    end
    end

And here is my new form

 <%= form_for :article, url: articles_path do |f| %>
   <% if params[:preview_button] %>
     <div id="preview">
         preview content...
     </div>
    <% end %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

  <%= submit_tag 'Preview', :name => 'preview_button' %>

Button when i click preview button nothing happen.Kindly help help me what i should do further.

Nithin
  • 3,679
  • 3
  • 30
  • 55

1 Answers1

3

Use the button inside the form

.........

  <p>
    <%= f.submit %>
    <%= f.submit "Preview" %>
  </p>
<% end %>

Then in create method

if params[:commit] == "Preview"
 # do the stuff
  else
 # create the object
end

check this

Community
  • 1
  • 1
Nithin
  • 3,679
  • 3
  • 30
  • 55