简体   繁体   English

Ruby on Rails编辑和删除与设计

[英]ruby on rails edit and delete with devise

When click on edit and destroy links, redirected to posts posts_path. 当单击“编辑并销毁”链接时,重定向到posts_path。 Edit form and destroy lonks are not working,. 编辑表格并销毁lonks无效。

posts_controller.html.erb 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 _form.html.erb

prohibited this post from being saved: 禁止保存该帖子:

<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 edit.html.erb

<h1>Editing Post</h1>

<%= render 'form' %>

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

show.html.erb 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>

Change below line for destroy in show.html.erb: 在show.html.erb中更改以下行进行销毁:

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

for edit: 编辑:

<%= link_to 'Edit', edit_post_path(@post) %>

in routes.rb 在routes.rb中

resources :posts

In controller 在控制器中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM