显示每个订单的按钮

时间:2016-05-16 14:30:48

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

我是RoR的新手,我目前正在尝试构建一个网络应用。 我有一个带有Post的用户的经典应用程序。

其他模型Online用于将帖子放在公共墙上,并且它与嵌套表单相关联,表示可用的作品。

因此,当用户在线推送他的帖子时,会为每个可用的片段生成一个表单。

现在,我想要做的是为每个部分创建一个按钮,这将改变可用部分的状态。

我的代码:

Posts_controller:



class PostsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  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
&#13;
&#13;
&#13;

Onlines_controller:

&#13;
&#13;
class OnlinesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_post 
  before_action :owned_online, only: [:new, :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 edit
  end

  def create 
      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 update  
    if @onlines.update(online_params)
      if @online.push == false
        if @online.portion <= 0
          @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, orders_attributes: [:id, :taked, :taked_at, :taked_by, :validated_at, :validated_by, :_destroy])
  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
&#13;
&#13;
&#13;

views / posts / show:

&#13;
&#13;
  <div class="col-md-9">
    <h3>Orders :</h3>
    <div id="ingredients">
      
      <ul>
        <%- @post.onlines.orders.each do |order| %>
        <li>
          <button>
          <%= order.id %>
          </button>
        </li>
        <%end%>
      </ul>
    </div>
  </div>
&#13;
&#13;
&#13;

所以,如果您有任何建议,我会接受它!
谢谢

1 个答案:

答案 0 :(得分:0)

除非Rails在我使用它之后添加了更多魔法,否则你需要在Posts控制器的“show”方法中分配你的@post变量。现在,@ show可能不存在于你的'show'视图中​​。

从那里开始,您的“发布”模型需要适当的关联才能达到“订单”。你取消引用你的@post对象(@ post.objects.orders)的方式表明一个帖子:has_many onlines和一个Online:has_many命令。您需要在Post和Online模型上设置这些关联。

我建议在这里:http://guides.rubyonrails.org/action_controller_overview.html#methods-and-actions 和这里: http://guides.rubyonrails.org/association_basics.html

从这一点开始。祝你好运!