marcamillion
marcamillion

Reputation: 33795

How do I render a new form instead of an edit on a show action?

I have a Post model, and depending on who is reading the Post#Show page I would like to change the form that is available.

How do I do that?

i.e. if a user.has_role? :guest, and they hit the form button, they should see a new (and empty) form.

If a user.has_role? :editor, and they hit that same form button they should see an edit form. However, they should be able to hit a "New Post" button that basically changes that "new form" into an "edit form".

I know this breaks Rails RESTFul ways, but how do I achieve this?

Edit 1

I am using Simple_Form, so this is what my form partial looks like:

<%= simple_form_for(@post, html: {class: 'form-horizontal' }) do |f| %> 
    <%= f.error_notification %>
    <%= f.input_field :parent_id, as: :hidden %>
    <div class="row">
        <div class="col-xs-12">
                    <% if can? :manage, @post %>
                    <label for="status">
                        Status
                            </label>
                            <%= f.input_field :status, label: "Status", collection: Post.statuses.keys, selected: :unconfirmed %>
                    <% end %>           

                        <% if can? :manage, @post %>
                <label for="title">
                    Title
                            </label>
                        <%= f.input_field :title, placeholder: "Enter Title"  %>
                            <span id="wordCountTitle">0</span> words<br/>
                            <span class="help-block">A block of help text that breaks onto a new line and may extend beyond one line.</span>            
                        <% end %>
                            <div class="report-field">
                    <label for="report">
                        Report
                                </label>
                    <div class="input-group">
                        <span class="input-group-addon"><span class="fa fa-file-text"></span>
                        </span>
                                    <%= f.input_field :body, id: "body-field", placeholder: "Provide all the facts of what happened, who did it, and where it happened.", class: "form-control", rows: "4" %><br />
                                </div>
                                <span class="help-block">Please avoid speculation.</span>                                           
                                <span id="wordCountBody">0</span> / 150 words
                            </div>
                            <!-- <br/>
                            <br /> -->
                            <!-- <br /> -->

                <label for="name">
                    Check if you are an eye witness:
                            </label>
                            <%= f.input_field :has_eyewitness, boolean_style: :inline %>
                            <br/>

                            <div>
                            <label for="photoFileUpload">Upload Images:</label>
                                <%= f.input_field :photo %>
                            <p class="help-block">Allowed: png, gif, jpg/jpeg</p>
                          </div>

                            <div>
                            <label for="FileUpload">Upload Documents, Video, or Audio:</label>
                                <%= f.input_field :file %>
                            <p class="help-block">Allowed: txt, pdf, doc, docx, xls, xlsx, mp4, m4v, mp3, wav.</p>
                          </div>
        </div>

        <% if can? :manage, @post %>
                <%= f.input :youtube_embed_code %> <br />
                <%= f.input :soundcloud_embed_code %> <br />
            <% end %>

    <div class="col-xs-12">
            <%= f.button :submit, class: "btn btn-primary pull-left" %>
    </div>          
    </div> <!-- //row -->
<% end %>

This is how this form is executed in my application.html.erb

<%= render "posts/form" %>

Upvotes: 0

Views: 69

Answers (1)

j-dexx
j-dexx

Reputation: 10416

Supply the form with a new instance of the Post model

<% if user.has_role? :guest %>
  <%= form_for Post.new do |f| %>
  <% end %>
<% elsif user.has_role :editor %> 
  <%= form_for @post do |f| %>
  <% end %>
<% end %>

I would definitely reconsider this though, past you knowing it breaks RESTful ways. Have you considered what happens if there are validation errors for the guest's post?

Upvotes: 1

Related Questions