javascript - 更新嵌套表单的路由

标签 javascript jquery mysql ruby-on-rails ruby

我是 RoR 新手,我正在尝试练习构建 Web 应用程序。我有一个经典的应用程序,用户有帖子。

另一个在线模型用于将帖子放在公共(public)墙上,它与代表可用件的嵌套表单 Orders 相关联。

现在,我正在尝试使用帖子显示 View 中的按钮更新嵌套表单(订单)。但它返回给我一个错误“没有路线匹配[POST]” 当我写一个 PUT 时。那么为什么他认为我想要下一个新订单(POST)?我怎样才能改变这一点?

我的代码:

路线:

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'

观看次数/帖子/节目:

  <div class="col-md-9">
    <h3>Orders :</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>

和我的在线 Controller :

 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: true, 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 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

在线:

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

&用户:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

         has_many :posts, dependent: :destroy
         has_many :onlines, dependent: :destroy
         has_many :comments
has_many :orders, dependent: :destroy

         validates :pseudo, presence: true, length: { minimum: 4, maximum: 16 }

         has_attached_file :avatar, styles: { medium: '100x100#' }  
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ 
end

错误: enter image description here 好吧,所以如果你有任何建议,我会接受的!谢谢

最佳答案

No route matches [POST]

在您的link_to中,您的method: :update是错误的。您需要将其更改为以下内容

<%= link_to 'Take', taked_online_path(online), method: :put, class: "btn btn-warning"%>

关于javascript - 更新嵌套表单的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37298030/

相关文章:

javascript - Webpack 输出是反向的

javascript - 使用 babel.js 而不是 browserify 编译成 bundle

当触发 keyup 时,Javascript 会忘记其他 keydown

php - CakePHP 管理索引

php - fatal error : Call to a member function prepare() on string

javascript - 通过 SSL 使用 JSONP

javascript - jquery 代码 fancybox 第二次显示错误

javascript - 单击页面任意位置时隐藏侧边栏

jquery - JQuery 显示的奇怪行为 - 按钮的顺序决定是否显示按钮

mysql - 在同一应用程序中将 codeigniter 连接到 mysql 和 oracle