单击提交后,Rails无法保存条目

时间:2018-12-29 16:10:31

标签: ruby-on-rails devise

在安装Devise gem之前,我的应用程序运行良好。安装Devise之后,当我在表单上单击“提交”时,它会重新加载同一页面,并且不会将条目保存到表中。它应该重定向到“询问”页面,询问用户是否还有更多条目。我已经从该站点尝试了多种解决方案,但是似乎没有一个可行,因此我想发布此问题以使其正常运行并了解正在发生的事情。命名可能会引起混淆。我的第一个模型是成员,具有字段输入,gifttext,配偶和spousegift。安装Devise之后,我为用户模型添加了电子邮件和加密密码字段。

我的members_controller.rb:

class MembersController < ApplicationController

include MembersHelper
def new
  @member = Member.new
end  
def index
    @members = Member.all
end
def destroy 
    @member = Member.find(params[:id])
    @members = Member.destroy(params[:id])
    redirect_to members_path
end
def create
   @member = Member.new(member_params)
if @member.save
  redirect_to :action => 'ask'
else
  render :action => 'new'
end
end
def ask
end
def show
end
def update
    @article = Member.find(params[:id])

    if @member.update(member_params)
        redirect_to @member
    else
        render 'edit'
        end
    end
def results
    @matches = prints
end
def member_params
      params.require(:member).permit(:input, :gifttext, :spouse, :spousegift)
end

end

我的应用程序控制器:

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
end

我的member.rb:

class Member < ApplicationRecord
    belongs_to :user
end

我的user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
end

我的_form.html.erb:

<%= form_with(model: @member, local: true ) do |f| %>
<p>Who is the participant?</p>
<div class="field">
    <%= f.label :person %>
    <%= f.text_field :input %>
</div>
<p>What gifts would they like?</p>
<div class="field">
  <%= f.label :gifts %>
  <%= f.text_field :gifttext %>
</div>
<p>Who, if anyone, is their spouse? If they don't have one you can leave this field blank</p>
<div class="field">
  <%= f.label :spouse %>
  <%= f.text_field :spouse %>
</div>
<p>What gifts would they like? If there is no spouse then you can leave this field blank.</p>
<div class="field">
  <%= f.label :gifts %>
  <%= f.text_field :spousegift %>
</div>
  <div class="action">
  <%= f.submit "Submit" %>
</div>
  <%= link_to 'Matches', members_results_path(@member) %>
<% end %>

我的路线.rb:

Rails.application.routes.draw do
  devise_for :users
  get 'members/new'
  get 'members/results'
  get 'members/index'
  get 'names/new'
  get 'names/results'
  get 'names/index'
  root 'members#new'
  post 'members/new'
  get 'members/ask'
  resources :members
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

我为用户模型的迁移:

# frozen_string_literal: true

class AddDeviseToUsers < ActiveRecord::Migration[5.2]
  def self.up
    change_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.inet     :current_sign_in_ip
      # t.inet     :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      # Uncomment below if timestamps were not included in your original model.
      # t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end

  def self.down
    # By default, we don't want to make any assumption about how to roll back a migration when your
    # model already existed. Please edit below which fields you would like to remove in this migration.
    raise ActiveRecord::IrreversibleMigration
  end
end

我的会员迁移:

class CreateMembers < ActiveRecord::Migration[5.2]
  def change
    create_table :members do |t|
      t.string :input
      t.string :gifttext
      t.string :spouse
      t.string :spousegift

      t.timestamps
    end
  end
end

让我知道是否需要其他信息。

1 个答案:

答案 0 :(得分:0)

您的MembersController#update方法使用一个@article实例变量。您可能是说@member

def update
  @member = Member.find(params[:id])

  if @member.update(member_params)
    redirect_to @member
  else
    render 'edit'
  end
end

您的创建操作看起来不太正确,或者您需要设置用户并正确重定向:

def create
  @member = Member.new(member_params)
  @member.user = current_user
  if @member.save
    redirect_to ask_member_path(@member)
  else
    render 'new'
  end
end

_form.html.erb

<%= @member.errors.inspect %>