Everything was working fine but I added stuff to my original scaffold, Specifically this line of code. "rails g migration add_first_name_and_last_name_to_users first_name:string last_name:string bio:text" After this I did "rake db:migrate" and added the fields. After working out the error I can't get past this new one.
Here is my users_controller.rb,
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
after_action :signed_in_after_register, only: :create
def index
@users = User.all
@user = User.find(session[:user_id])
end
def dashboard
@user = User.find(session[:user_id]) unless session[:user_id] == ""
redirect_to login_path, notice: "You're not logged in" unless @user
@posts = @user.posts.order("created_at DESC").limit(3)
end
def newsfeed
@user = User.find(session[:user_id]) unless session[:user_id] == nil
redirect_to login_path, notice: "You're not logged in" unless @user
end
def nav
@user = User.find(session[:user_id])
end
def destroy
@user = User.find(session[:user_id])
end
def welcome
@user = User.find(session[:user_id])
end
def show
@user = User.find(session[:user_id]) unless session[:user_id] == ""
redirect_to login_path, notice: "You're not logged in" unless @user
@posts = @user.posts.order("created_at DESC").limit(3)
end
def new
@post = Post.new(params[:post_id])
@user = User.new
end
def edit
end
def create
@post.user = @current.user
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to profile_path, 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
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to dashboard_path, 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
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
def set_user
@user = User.find(params[:id])
end
def signed_in_after_register
session[:user_id] = @user.id
end
def user_params
params.require(:user).permit(:first_name, :last_name, :bio, :name, :password, :password_confirmation, :email, :age, :profile_picture, :post)
end
end
This is in my _form.html.erb
<%= form_for @user, html: { multipart: true } do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="sign_up_form_2">
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name, placeholder: "First Name", class: "form-control" %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name, placeholder: "Last Name", class: "form-control" %>
</div>
<div class="field">
<%= f.label :User_name %><br>
<%= f.text_field :name, placeholder: "User Name", class: "form-control" %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email, placeholder: "Email", class: "form-control" %>
</div>
<div class="field">
<%= f.label :age %><br>
<%= f.number_field :age, placeholder: "Age", class: "form-control" %>
</div>
<div class="field">
<%= f.label :password %><br>
<%= f.password_field :password, placeholder: "Password", class: "form-control" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br>
<%= f.password_field :password_confirmation, placeholder: "Password Confirmation", class: "form-control" %>
</div><br>
<div class="field">
<%= f.label :profile_picture %>
<%= f.file_field :profile_picture %><br>
</div>
<div class="actions">
<%= f.submit "Create Account", class: "btn btn-default btn-sign-up" %>
</div>
<% end %>
</div>
If there is any other code you need to see to get a better understand then please let me know! I've been stuck on this for an hour now and I'm sure its pretty simple, Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire