上传我的数据库时出错

时间:2016-04-22 18:44:09

标签: ruby-on-rails database migration

我的代码有点问题,我从来没有看到那种错误,我在第二页编辑我的帖子(例如像设计的个人资料页面)。所以,当我想要编辑帖子时,它总是我的数据库的第一篇文章,它接受了更改信息。

所以如果你能看到我做了哪个可怕的错误,那就太棒了!!

路线:



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

  get 'pushs/edit'

  get 'profiles/show'

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

  resources :posts do 
    resources :comments
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

  get 'push/:post_id', to: 'pushs#show', as: :push
  get 'push/:post_id/edit', to: 'pushs#edit', as: :edit_push
  patch 'push/:post_id/edit', to: 'pushs#update', as: :update_push

  root 'posts#index'




推送控制器:



class PushsController < ApplicationController
  
  before_action :authenticate_user!
  before_action :set_push

  def show
    @post = Post.find_by(params[:post_id])
  end

  def edit
  end


 def update
  
    if @post.update(push_params)
      flash[:success] = 'Your profile has been updated.'
      redirect_to root_path
    else
      @post.errors.full_messages
      flash[:error] = @post.errors.full_messages
      render :edit
    end
  end

  private

  def push_params
    params.require(:post).permit(:prix, :portion)
  end


  def owned_push 
    @post = Post.find_by(params[:id])
    unless current_user == @post.user_id
      flash[:alert] = "That prost doesn't belong to you!"
      redirect_to root_path
    end
  end


  def set_push
    @post = Post.find_by(params[:id])
  end



end
&#13;
&#13;
&#13;

修改视图(推送)

&#13;
&#13;
<h1>Mettre le Plat en ligne :</h1>


<div class="posts-wrapper">
  <div class="post">
    <div class="post-body">
      <div class="image-wrap">
        <%= form_for @post, url: {action: "update"}, html: { multipart: true } do |f| %>
          <div class="row">
            <div class="col-md-12">
              <div class="form-group text-center">
                <h4>prix</h4>
                <%= f.text_area :prix %>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-md-12">
              <div class="form-group text-center">
                <h4>portion</h4>
                <%= f.text_area :portion %>
              </div>
            </div>
          </div>
          <div class="form-group text-center">
            <%= f.submit "Update Post", class: 'btn btn-success' %>
          </div>
        <% end %>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

如果您想了解更多信息,可以在github上查看我的代码: https://github.com/Mesnet/Beta0.1

1 个答案:

答案 0 :(得分:0)

您正在使用参数post_id查找@post = Post.find(params[:id]) ,但在编辑页面上,如果您有传统的rails应用程序,那么您通常会使用的参数只是id。

尝试添加:

{{1}}

在更新操作的第一行。如果不解决,请告诉我。并且请更新您的问题以包括发布表单时堆栈跟踪中出现的params哈希值。