小编use*_*806的帖子

PostsController中的ActiveModel :: ForbiddenAttributesError #create

我已经开始学习Ruby on Rails了.

我得到以下错误: ActiveModel::ForbiddenAttributesError in PostsController#create

这是代码:

posts_controller.rb

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def show
    @post = Post.find params[:id]
  end

  def new
    @post = Post.new
  end

  def create
    @post =  Post.create(params[:post]) // Its showing me error here

    if @post.save
      redirect_to posts_path notice: "Your Post was saved"
    else
      render "new"
    end
  end

end
Run Code Online (Sandbox Code Playgroud)

new.html.erb

<h1> Add a New Post </h1>

<%= form_for @post do |f| %>
  <p>
    <%= f.label :title %><b/>
    <%= f.text_field :title %><b/> …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-4

1
推荐指数
2
解决办法
2957
查看次数

ruby nil让我困惑

我从昨天开始学习Ruby.以下是我的疑问:

class MegaGreeter
      attr_accessor :names

      def intialize(names="World!")
        @names = names
      end

      def say_test
        if @names.nil?
          puts "nil test..."
        end
      end  

    end


    megaGreeter = MegaGreeter.new
    megaGreeter.say_test // Here showing ouput as "nil test..." 
Run Code Online (Sandbox Code Playgroud)

起初我只是assuming 'nil' almost like 'null' in java.但我的假设在上述情况下似乎是错误的.

我不明白为什么在上面if @names.nil returning as TRUE?,@names will have a value "World!"既然如此,@ names如何视为null?

我已经搜索了' nil',但我真的没有清楚地了解它.

ruby ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

0
推荐指数
1
解决办法
42
查看次数