2016-05-17 9 views
-1

私はRoRで新しく、Webアプリケーションの構築を練習しようとしています。 私はポストを持っているユーザーと古典的なアプリを持っています。ネストされたフォームを更新する

他のモデルオンラインは一般的な壁にポストを置くために使用され、利用可能な部分を表す入れ子式の注文に関連付けられています。

私は、私のポストショーのビューで "taked"というアクションでOrdersを更新しようとしていますが、レールには、プライベートメソッドがオンラインで設定されている「id」=注文を作成する。 (写真)

エラー: マイコード:

オンラインにコントローラ:

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

は、私はこの問題は、その行から来ていると思います。<%= LINK_TO、方法(オンライン)taked_online_path「を取る」:更新、クラスを「BTN BTN-警告」%> しかし、私は、属性に –

+0

で私のオンラインコントローラに –

答えて

1

はあなたがparams{:post_id]の助けを借りてをトリガしてしまったことcntrollerの任意のアクションを@postを割り当てbefore_action :set_onlineを持っている 'ID' =

でポストを見つけることができませんでした。しかし、takedアクションのためにpost_idが来ないので、が失敗します。このエラーを報告してください。あなたはbefore_action :set_online, except: [:taked]からbefore_action :set_onlineを変更することにより、このチェックを回避することができますが、これは再び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

うん、それはプットになりましたが、エラーは、サーバーを再起動してみてください@lilipupu POST –

関連する問題