ruby on rails编辑和删除设计

时间:2017-04-28 06:38:13

标签: ruby-on-rails ruby devise

点击编辑并销毁链接后,重定向到帖子posts_path。编辑表单并销毁lonks无效,。

posts_controller.html.erb

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  before_action :correct_user, only: [:edit, :update, :destroy]
  # GET /posts
  # GET /posts.json


  def index

    if params[:tag]
      @posts = Post.tagged_with(params[:tag])
    else
      @posts = Post.search(params[:search])
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = current_user.posts.build
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = current_user.posts.build(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.friendly.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :description, :html, :css, :js, :image, :tag_list, :slug)
    end

    def correct_user
      @post = current_user.posts.find_by(id: params[:id])
      redirect_to posts_path, notice: "Not authorized to edit" if @post.nil?
    end

end

_form.html.erb

                          

禁止保存此帖子:

          
                           
  •                      
               
<div class="col-md-6 col-md-offset-3">
  <div class="field">
    <%= f.text_field :title, class: "form-control", placeholder: "Title" %>
  </div><br>
  <div class="field">
    <%= f.text_field :tag_list, class: "form-control", placeholder: "Tags seperated with commas" %>
  </div><br>
  <div class="field">
    <%= f.file_field :image, as: :file, class: "form-control" %>
  </div><br>
  <div class="field">
  <%= f.label :Description %>
    <%= f.cktext_area :description, class: "form-control", placeholder: "preview link" %>
  </div><br>
  <div class="field">
  <%= f.label :html %>
    <%= f.cktext_area :html, class: "form-control", placeholder: "preview link" %>
  </div><br>
  <div class="field">
  <%= f.label :css %>
    <%= f.cktext_area :css, class: "form-control", placeholder: "download link" %>
  </div><br>
  <div class="field">
  <%= f.label :js %>
    <%= f.cktext_area :js, class: "form-control" %>
  </div><br>



  <div class="actions">
    <%= f.submit %>
  </div>
  </div>

<% end %>

edit.html.erb

<h1>Editing Post</h1>

<%= render 'form' %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>

show.html.erb

<div class="col-md-8 col-md-offset-2">

<div class="row">

<div>
  <strong id="title"><%= @post.title %></strong><br>
  <p>Published on <%= @post.created_at.strftime('%F') %></p>
</div><hr>

<div style="border: 2px solid #f1f1f1;">
  <%= image_tag @post.image.url(:medium), class: "img-responsive" %>
</div>
<br><br>
<div id="des">
      <%= raw @post.description %>
      </div>

<div class="">
 <ul class="nav nav-tabs">

    <li><a class="active" data-toggle="tab" href="#menu1">HTML</a></li>
    <li><a data-toggle="tab" href="#menu2">CSS</a></li>
    <li><a data-toggle="tab" href="#menu3">JS</a></li>
  </ul>

  <div class="tab-content" style="overflow: auto; background-color: #f5f5f5;">

    <div id="menu1" class="tab-pane active">
      <div id="des">
      <%= raw @post.html %>
      </div>
    </div>
    <div id="menu2" class="tab-pane fade">
      <div id="des">
      <%= raw @post.css %>
      </div>
    </div>
    <div id="menu3" class="tab-pane fade">
     <div id="des">
      <%= raw @post.js %>
      </div>
    </div>
  </div>
</div>  
<br>
<div><i class="fa fa-tags"></i> <%= raw @post.tag_list.map { |t| link_to t, tag_path(t)}.join(', ') %></div>

<hr>
<div class="text-center">
<a onclick="javascript:window.open('http://facebook.com/share?text=<%= @post.title %> by Ganesh Raju - &amp;<%= url_for([@post, {only_path: false}]) %>', '_blank', 'width=800, height=500, top=200, left=300');void(0);"><i class="fa fa-lg fa-facebook"></i></a>
<a onclick="javascript:window.open('http://twitter.com/share?text=<%= @post.title %> by @ganesh12gani - &amp;<%= url_for([@post, {only_path: false}]) %>', '_blank', 'width=800, height=500, top=200, left=300');void(0);"><i class="fa fa-lg fa-twitter"></i></a>
<a onclick="javascript:window.open('http://linkedin.com/share?text=<%= @post.title %> by Ganesh Raju - &amp;<%= url_for([@post, {only_path: false}]) %>', '_blank', 'width=800, height=500, top=200, left=300');void(0);"><i class="fa fa-lg fa-linkedin"></i></a>

</div>
<hr>
<div>
  <%= render 'disqus' %>
</div>
</div>
<div>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
<%= link_to 'Destroy', @post, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

在show.html.erb中更改以下行以进行销毁:

<%= link_to 'Destroy', post_path(@post), method: :delete, data: { confirm: 'Are you sure?' } %>

用于编辑:

<%= link_to 'Edit', edit_post_path(@post) %>
routes.rb 中的

resources :posts

在控制器

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  before_action :correct_user, only: [:edit, :update, :destroy]

...

private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.friendly.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :description, :html, :css, :js, :image, :tag_list, :slug)
    end

    def correct_user
      #@post = current_user.posts.find_by(id: params[:id])
      redirect_to posts_path, notice: "Not authorized to edit" if @post.user.id != current_user.id
    end

end