Désactivate最早的帖子

时间:2016-05-11 12:34:31

标签: javascript jquery sql ruby-on-rails ruby

我是rails的新手,我正在尝试构建一个Web应用程序,但我遇到了我的能力极限。 我有一个经典的应用程序,用户有帖子,并将这篇文章公开,我创建了一个模型和一个控制器在线。 我现在正在尝试,如果使用与之前相同的post_id创建新的Online,则更改布尔值:push(来自Online)为false。 因此,如果我再次推送我的帖子2,与帖子2关联的在线预览将更新他们:推入错误。 好吧,如果您有任何想法,请告诉我! 谢谢

我的代码=

控制器:

  • Onlines:

class OnlinesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_post 
  before_action :owned_online, only: [:new, :edit, :update]
  before_action :set_online

  def new 
@online = current_user.onlines.build
@online.post_id = @post.id
@online.user_id = current_user.id
  end 

  def create 
@online.user_id = current_user.id
@online.post_id = @post.id 
  if Online.where(post_id: params[:post_id]).any?
   @online = Online.where(post_id: params[:post_id]).last.update_attributes(push: false)
  end
 @online = @post.onlines.create(online_params)
if @online.save
  if @online.portion <= 0
     @online.update(push: false)
     flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
     redirect_to root_path 
  else
   @online.update(pushed_at: Time.zone.now)
   @online.update(push: true)
   flash[:success] = 'Votre post est en ligne !'
   redirect_to root_path
  end
else 
  render 'new'
end 
  end 

  def show 

  end 

def update  
    if @onlines.update(online_params)
      if @online.push == false
        if @online.portion <= 1
          @online.update(push: false)
          flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
          redirect_to root_path 
        else
         @online.update(push: true)
      
      flash[:success] = 'Votre post a bien été pushé !'
      redirect_to root_path      
      end   
    end
    else
      @user.errors.full_messages
      flash[:error] = @user.errors.full_messages
      render :edit
    end
  end


private 

def online_params
  params.require(:online).permit(:user_id, :post_id, :prix, :portion, :push, :pushed_at)
  end 

  def owned_online 
     @post = Post.find(params[:post_id])
  unless current_user == @post.user
    flash[:alert] = "That post doesn't belong to you!"
    redirect_to :back
  end
end  

  def set_post
  @post = Post.find_by(params[:post_id]) 
  end 


  def set_online
    @post = Post.find(params[:post_id])
  @online = Online.find_by(params[:id]) 
  end 

end

帖子:

class PostsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :set_online
  before_action :owned_post, only: [:edit, :update, :destroy]
  

  # GET /posts
  # GET /posts.json
  def index
        @posts = Post.push_posts 

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.find(params[:id])
    end

   
    

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:user_id, :title, :description, :image, ingredients_attributes: [:id, :name, :_destroy])
    end

    def owned_post  
  unless current_user == @post.user
    flash[:alert] = "That post doesn't belong to you!"
    redirect_to root_path
  end
end  

def set_online
  @onlines = Online.find_by(params[:id]) 
  end 

end

模特

  • 在线:

class Online < ActiveRecord::Base
  
  belongs_to :post
  belongs_to :user

  
  scope :push, ->{ where(push: true).order("pushed_at DESC") }
  

end

  • 发布:

  class Post < ActiveRecord::Base

  
  belongs_to :user
  has_many :onlines, dependent: :destroy

  scope :push_posts, lambda { joins(:onlines).merge(Online.push) } 

  has_many :ingredients, dependent: :destroy
  


  accepts_nested_attributes_for :ingredients, reject_if: :all_blank, allow_destroy: true

  has_many :comments
  
  validates :image, presence: true

  has_attached_file :image, styles: { medium: "300x300#"}, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

&安培;最后,显示帖子索引的视图:

 
<div class="row">
<%- @posts.each do |post|%>
<%- if post.onlines.last.push == true %>
<div class="post">
  <div class="form-group text-center">
    <h3> <%=post.title%></h3>
  </div>

  
 <p> Posted by : <%= link_to post.user.pseudo, profile_path(post.user.pseudo) %>,  <%= time_ago_in_words(post.onlines.last.pushed_at) %> ago </p>



<p><%= post.onlines.last.prix %></p>
<p><%= post.onlines.last.portion %></p>

  <div class="image text-center">
    <div class="image-border">
     <%= link_to (image_tag post.image.url(:medium), class: 'img-responsive'), post_path(post)%>
    </div>
  </div>



  </div> 
  <% end %>
  <%end%>
  </div>
 </div>
 </div>

谢谢你的时间!!

1 个答案:

答案 0 :(得分:0)

您可能会将其置于可以使用

的控制器操作中

Other

或类似的东西。如果您使用相同的YourModel.where(post_id: params[:post_id]).update_all(push: false)

创建两个以上的项目,这取决于您想要做什么