简体   繁体   中英

Ruby on rails undefined method `firstname'

I am working on the final project "stories" on code academy and my signup page is throwing this error code

undefined method 'firstname' for #<Signup id: nil, created_at: nil, updated_at: nil>

Any help to fix this error would be greatly appreciated.

Here is my html

    <div class="contact-main">
  <img alt="Open" class="icon-menu" src="/assets/open.svg" width="24px" />
  <div class="content container">
    <h1>Get the latest updates from Stories</h1>
    <div class="row">
      <div class="col-md-4 col-md-4-offset"></div>
      <div class="col-md-4">
        <%= form_for(@signup) do |f| %>
          <div class="form-group">
            <%= f.label "First Name" %>
            #<%= f.text_field :firstname, { :class => "form-control" } %>
          </div>
          <div class="form-group">
            <%= f.label "Email" %>
           # <%= f.text_field :email, { :class => "form-control" } %>
          </div>
        #  <%= f.submit "Join", { :class => "btn btn-default" } %>
        <% end %>
      </div>
      <div class="col-md-4 col-md-4-offset"></div>
    </div>
  </div>
</div>

Controller_

class SignupsController < ApplicationController
    def new
        @signup = Signup.new
    end
    def create
        @signup = Signup.new(signup_params)
        if @signup.save
            redirect_to "/thanks"
        else
            render "/new"
        end
    end
    def thanks
    end
    private
    def signup_params
        params.require(:signup).permit(:firstname).permit(:email)
    end
end

And finally my migration

class CreateSignups < ActiveRecord::Migration
  def change
    create_table :signups do |t|
      t.string :firstname
      t.string :email
      t.timestamps null: false
    end
  end
end

Hey when you permit your params, you can do that

params.require(:signup).permit(:firstname, :email)

Instead of that:

params.require(:signup).permit(:firstname).permit(:email)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM