2014-11-12 3 views
0

나는 Post 모델을 가지고 있으며 누가 Post#Show 페이지를 읽고 있는지에 따라 사용 가능한 양식을 변경하고 싶습니다.표시 작업에서 편집 대신 새 양식을 렌더링하는 방법은 무엇입니까?

어떻게하면됩니까?

즉, user.has_role? :guest 인 경우 양식 버튼을 누르면 새로운 (빈) 양식이 표시됩니다.

user.has_role? :editor과 같은 양식 버튼을 누르면 편집 양식이 표시됩니다. 그러나 기본적으로 "새 양식"을 "편집 양식"으로 변경하는 "새 게시물"단추를 눌러야합니다.

나는 이것이 RailsRESTFul 방식을 어기는 것을 알고 있지만 어떻게해야합니까?

편집 한

나는 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
에서 실행하는 방법입니다

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 방법을 아는 지나서 이것을 다시 생각할 것이다. 손님의 게시물에 대한 유효성 검사 오류가있는 경우 어떻게 될지 고려 했습니까?

+0

유효성 검사 오류가 있으면 정상적인'Post # New'가 처리하는 것처럼 처리하지 말아야합니까? Rails 컨벤션 내에서 내가 성취하고자하는 것을 어떻게 달성 할 수 있습니까? – marcamillion

+0

괜찮 으면 괜찮을거야. –

+0

좋습니다. 예, 괜찮습니다. – marcamillion

관련 문제