我是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'
所以,如果您有任何意见的,我”会采取它! 感谢
你究竟在哪里得到这个'找不到带'id'='错误的帖子? – Pavan
在我的在线控制器中,使用私有方法 –
我认为问题来自该行: <%= link_to'Take',taked_online_path(online),method :: update,class:“btn btn-warning”%> 但我找不到如何更新属性 –