2016-05-17 125 views
-1

我是RoR中的新成員,並且正在嘗試構建Web應用程序。 我有一個有Post的用戶的經典應用程序。更新嵌套表單

另一個模型Online用於將帖子放在公共牆上,並且它與嵌套表單相關聯表示可用塊的訂單。

所以現在,我試圖更新訂單與行動「在我的後顯示視圖中」,但鐵軌說,他無法找到與'id'= Post與私人方法設置聯機工作創建訂單。 (照片)

錯誤: 我的代碼:

人在線控制器:

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

 

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

 
    def edit 
 
    end 
 

 
    def taked 
 
    @online.orders.update(taked: false, taked_at: Time.zone.now, taked_by: current_user) 
 
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 
 

 
    def set_unline 
 
    @online = Online.find_by(params[:id]) 
 
end 
 

 
end

class Online < ActiveRecord::Base 
 
    
 
    belongs_to :post 
 
    belongs_to :user 
 
    has_many :orders 
 
    
 
    accepts_nested_attributes_for :orders, allow_destroy: true 
 
    
 
    scope :push, ->{ where(push: true).order("pushed_at DESC") } 
 
end

查看/職位/顯示:

<div class="btn-group" role="group" aria-label="..."> 
 
    <%= link_to '- Pusher - ', new_post_online_path(@post), data: { confirm: 'Confirmer la mise en ligne de #{@title}?' }, class: "btn btn-primary " %> 
 

 

 
    <div class="col-md-9"> 
 
    <h3>Parts :</h3> 
 
    <div id="Orders"> 
 
     
 
     <ul> 
 
    <%- @post.onlines.each do |online| %> 
 
     <%- online.orders.each do |order| %> 
 
     <%- if order.taked == false %> 
 
     <li> 
 
    <%= link_to 'Take', taked_online_path(online), method: :update, class: "btn btn-warning"%> 
 
     </li> 
 
     <%end%> 
 
     <%end%> 
 
    <%end%> 
 
    </ul> 
 
    </div> 
 
    </div>

而且路線:

Rails.application.routes.draw do 
 
    get 'profiles/show' 
 

 
    mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 
 
    
 
    devise_for :users, :controllers => { registrations: 'registrations' } 
 

 
    resources :posts do 
 
    resources :comments 
 
    resources :onlines do 
 
     resources :orders 
 
    end 
 
end 
 

 
    get ':pseudo', to: 'profiles#show', as: :profile 
 
    get ':pseudo/edit', to: 'profiles#edit', as: :edit_profile 
 
    patch ':pseudo/edit', to: 'profiles#update', as: :update_profile 
 

 
put 'online/:id/taked', to: 'onlines#taked', as: :taked_online 
 

 
    
 
    
 

 
    root 'posts#index'

所以,如果您有任何意見的,我」會採取它! 感謝

+0

你究竟在哪裏得到這個'找不到帶'id'='錯誤的帖子? – Pavan

+0

在我的在線控制器中,使用私有方法 –

+0

我認爲問題來自該行: <%= link_to'Take',taked_online_path(online),method :: update,class:「btn btn-warning」%> 但我找不到如何更新屬性 –

回答

1

無法與 'ID'=

找到後你有before_action :set_online這之前params{:post_id]幫助分配@post是cntroller的任何行動得到了觸發。但沒有post_idtaked行動,所以它失敗報告錯誤。你可以通過改變before_action :set_onlinebefore_action :set_online, except: [:taked]避免這種檢查,但這個再沒有爲taked作用,這需要該方法分配@online

所以,只爲你的選擇是除去在set_online方法分配@post

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

此外,您的代碼需要一些更改。首先,路線對於taked_online是錯誤的。它應該是

put 'online/:id/taked', to: 'onlines#taked', as: :taked_online 

因爲你是用它來更新,不爲創建

最後,taked方法需要調整。

def taked 
    @online.orders.update(taked: false, taked_at: Time.zone.now, taked_by: current_user) 
end 
+0

感謝您的回覆,我已經應用了您的所有建議,我認爲我離目標很遠。我複製了set_online方法,以便刪除@post。但是,我仍然有錯誤隱瞞了更改後的路線,錯誤說「沒有路線匹配[POST]」/ online/248/taked「」。 那麼爲什麼他認爲我想在我的代碼中創建一個新的在線? –

+0

@lilipupu您是否根據我的更改更改了路線? – Pavan

+0

是的,它現在在放,但錯誤仍然在POST –