如何在show动作中呈现新表单而不是编辑?

时间:2014-11-12 13:08:35

标签: ruby-on-rails ruby-on-rails-4

我有一个Post模型,根据谁正在阅读Post#Show页面,我想更改可用的表单。

我该怎么做?

即。如果是user.has_role? :guest,并且他们点击了表单按钮,则会看到一个新的(和空的)表单。

如果是user.has_role? :editor,并且他们点击相同的表单按钮,则会看到编辑表单。但是,他们应该能够点击“新帖子”按钮,基本上将“新表格”改为“编辑表格”。

我知道这会破坏Rails RESTFul方式,但我该如何实现呢?

修改1

我正在使用Simple_Form,所以这就是我的表单部分看起来的样子:

<%= 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 %>

这是我application.html.erb

中执行此表单的方式
<%= render "posts/form" %>

1 个答案:

答案 0 :(得分:1)

为表单提供Post模型的新实例

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

我肯定会重新考虑这个,过去你知道它打破了RESTful方式。您是否考虑过如果客人的帖子存在验证错误会发生什么?