NoMethodError - 未定义的方法`set'

时间:2017-07-11 05:42:25

标签: ruby-on-rails

No method error

嗨,当我尝试在我的数据库中显示第一个用户时,我遇到了这个NoMethodError。我的用户控制器如下:

class UsersController < ApplicationController
  before_action :set_, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
    @user = User.find(params[:id])
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
  if @user.save
    format.html { redirect_to @user, notice: 'User was successfully created.' }
    format.json { render :show, status: :created, location: @user }
  else
    format.html { render :new }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end
end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @users.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully 
updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end
end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully 
destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white 
list through.
def user_params
  params.require(:user).permit(:name, :email)
end
end

我的views / users / show基本上只打印用户名和电子邮件

<%= @user.name %><% @user.email %>

4 个答案:

答案 0 :(得分:1)

修复此问题

before_action :set_

before_action :set_user

答案 1 :(得分:0)

  

NoMethodError - 未定义的方法`set _&#39;对

错误来自

#include <iostream>

#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_cloud.h>
#include <pcl/console/parse.h>
#include <pcl/common/transforms.h>
#include <pcl/visualization/pcl_visualizer.h>

// This function displays the help
void showHelp(char *program_name)
{
    std::cout << std::endl;
    std::cout << "Usage: " << program_name << " cloud_filename.[pcd|ply]" << std::endl;
    std::cout << "-h: Show this help." << std::endl; 
}

// Main function
int main(int argc,char **argv)
{
    // Show help
    if(pcl::console::find_switch(argc,argv,"-h") || pcl::console::find_switch(argc,argv,"--help"))
    {
        showHelp(argv[0]);
        return 0; 
    }

    // Fetch point cloud filename in arguments | Works with PLY files
    std::vector<int> filenames;

    filenames = pcl::console::parse_file_extension_argument(argc,argv,".ply");



    // Visualization 
    printf("\n Point cloud colors :\n"
        " \t white \t = \t original point cloud \n");

    pcl::visualization::PCLVisualizer viewer(" Point Cloud Visualizer");
    viewer.setBackgroundColor(0.05,0.05,0.05,0); // Set background to a dark grey

    // Load file | Works with PLY files
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr source_cloud (new pcl::PointCloud<pcl::PointXYZRGB> ());

for(int i=0; i<argc-1; i++)
{

cout<<argv[filenames[i]]<<endl;

        if(pcl::io::loadPLYFile(argv[filenames[i]],*source_cloud) < 0)
        {
            std::cout << "Error loading point cloud " << argv[filenames[i+1]] << std::endl << std::endl;
            showHelp (argv[i+1]);
            return -1;
        }


    // Define R,G,B colors for the point cloud 
    pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(source_cloud);
//  pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> source_cloud_color_handler(source_cloud,255,255,255); // White

    // We add the point cloud to the viewer and pass the color handler 
if(i!=0){
    viewer.removePointCloud("original_cloud"+(i-1));
}
    viewer.addPointCloud(source_cloud,rgb,"original_cloud"+i);
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,2,"original_cloud"+i);

viewer.spinOnce(100, true);
    }

        return 0;
    } // End main()

可能你的意思是before_action :set_, only: [:show, :edit, :update, :destroy]

答案 2 :(得分:0)

替换此行
before_action :set_, only: [:show, :edit, :update, :destroy]

before_action :set_user, only: [:show, :edit, :update, :destroy]

答案 3 :(得分:0)

我猜你在这里粘贴的代码中有两个错误。

before_action :set_user, only: [:show, :edit, :update, :destroy]

并且在'update'方法中,对象是'@user'而不是'@users'。请更改

@users.update(user_params)

@user.update(user_params)