【问题标题】:Rails: Param is missing or the value is empty: discussionRails:缺少参数或值为空:讨论
【发布时间】:2015-02-03 19:53:34
【问题描述】:

讨论属于项目。当我尝试编辑讨论时,我收到以下错误。 param is missing or the value is empty: discussion 在讨论控制器中的以下代码:

def discussion_params
    params.require(:discussion).permit(:title, :description)
end 

但我看不出有什么问题。我的路由有问题还是我遗漏了什么?

discussions_controller.rb

class DiscussionsController < ApplicationController

  before_action :find_project, expect: [:destroy]

  def new
    @discussion = Discussion.new
  end

  def create
    @discussion = @project.discussions.build(discussion_params)
    if @discussion.save
      redirect_to new_project_discussion_path(@project)
    end
  end

  def edit
    @discussions = @project.discussions.find(discussion_params)
  end

  def update

    if @discussions.update_attributes(discussion_params)
      redirect_to new_project_discussion_path
    else
      render "edit"
    end
  end

  def destroy
    @discussions.destroy
    redirect_to new_project_discussion_path
  end


  private

  def discussion_params
    params.require(:discussion).permit(:title, :description)
  end

  def find_project
    @project = Project.find(params[:project_id])
  end

end

projects_controller.rb

class ProjectsController < ApplicationController

  def index
    @projects = Project.all
  end

  def show


  end

  def new
    @projects = Project.new

  end

  def create #no view
    @projects = Project.new(project_params)
    if @projects.save
      redirect_to projects_path, :notice => "Your project was sent!"
    else 
      render "new"
    end
  end

  def edit
    @projects = Project.find(params[:id])
  end

  def update #no view
    @projects = Project.find(params[:id])

    if @projects.update_attributes(project_params)
      redirect_to projects_path, :notice => "Your project has been updated."
    else
      render "edit"
    end
  end

  def destroy #no view
    @projects = Project.find(params[:id])
    @projects.destroy
    redirect_to projects_path, :notice => "Your project has been deleted."
  end

  private
  def project_params
    params.require(:project).permit(:title, :description)
  end


end

(讨论)_form.html.erb

<%= form_for [@project, @discussion] do |f| %>
  <div class="container">

    Project: <%= @project.title %> <%= link_to "Go back?", projects_path %> 
    <hr>
    <div class="form-group">
      <%= f.label :title %>
      <%= f.text_field :title, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :description %>
      <%= f.text_area :description, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.submit "Submit discussion", class: "btn btn-primary" %>
    </div>
  </div>
<% end %>

(讨论)edit.html.erb

<%= render "form" %>

(讨论)new.html.erb

<div class="container">
  <div class="page-header">
    <h1>Discussions<small> Discuss the project.</small></h1>
  </div>
</div>


<%= render "form" %>


<% if !@project.discussions.blank? %>
  <% for item in @project.discussions %>
  <div class="container">
  <div class="panel panel-default">
  <div class="panel-heading">
    <%= item.title %>
  </div>
  </div>
  <div class="panel-body">
  <p>
    <%= item.description %> <br>
    <%= link_to "Comment", new_discussion_comment_path(item) %>
    <%= link_to "Delete", item, :method => :delete %> 
    <%= link_to "Edit", edit_project_discussion_path(@project, item) %> |
  </p>
  </div>
  </div>
  <% end %>
<% end %>

这是我的讨论和项目路线。

project_discussions GET    /projects/:project_id/discussions(.:format)             discussions#index
                         POST   /projects/:project_id/discussions(.:format)             discussions#create
  new_project_discussion GET    /projects/:project_id/discussions/new(.:format)         discussions#new
 edit_project_discussion GET    /projects/:project_id/discussions/:id/edit(.:format)    discussions#edit
      project_discussion GET    /projects/:project_id/discussions/:id(.:format)         discussions#show
                         PATCH  /projects/:project_id/discussions/:id(.:format)         discussions#update
                         PUT    /projects/:project_id/discussions/:id(.:format)         discussions#update
                         DELETE /projects/:project_id/discussions/:id(.:format)         discussions#destroy
                projects GET    /projects(.:format)                                     projects#index
                         POST   /projects(.:format)                                     projects#create
             new_project GET    /projects/new(.:format)                                 projects#new
            edit_project GET    /projects/:id/edit(.:format)                            projects#edit
                 project GET    /projects/:id(.:format)                                 projects#show
                         PATCH  /projects/:id(.:format)                                 projects#update
                         PUT    /projects/:id(.:format)                                 projects#update
                         DELETE /projects/:id(.:format)                                 projects#destroy

【问题讨论】:

  • 实际发布了哪些参数(来自您的服务器日志)?现在,讨论键不见了。
  • 嗯,当我单击编辑时,我会在终端中收到此消息。 ActionController::ParameterMissing (param is missing or the value is empty: discussion): app/controllers/discussions_controller.rb:38:in 'discussion_params' app/controllers/discussions_controller.rb:17:in 'edit'
  • 你能安装 pry 调试器 gem 并在 params.require 之前放一个 binding.pry 吗?
  • 我已经更新了我的答案。请检查。

标签: ruby-on-rails ruby ruby-on-rails-4 routing


【解决方案1】:

当我单击编辑时,我会在终端中收到此消息。 ActionController::ParameterMissing(参数丢失或值为 空:讨论):app/controllers/discussions_controller.rb:38:in 'discussion_params' 应用程序/控制器/discussions_controller.rb:17:in '编辑'

错误背后的原因在于您的edit 方法中的discussions_controller.rb 你有这一行

@discussions = @project.discussions.find(discussion_params)

你需要改成

@discussion = Discussion.find(params[:id])

并且还将您的更新操作更改为以下内容。

def update
  @discussion = Discussion.find(params[:id])
   if @discussion.update_attributes(discussion_params)
     redirect_to new_project_discussion_path
   else
     render "edit"
   end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多