简体   繁体   中英

Rails wicked gem

I am having problems related to the links given to login and logout.

I am not using devise gem

In my code I have given the following links

<% if current_user %>
  <li><%= link_to 'Logout',{:controller=>'sessions', :action=> 'destroy'}%></li>
<% else %>
  <li> <%= link_to 'Signup',{:controller =>'users', :action => 'new'} %> </li>
  <li> <%= link_to 'Login,{:controller =>'sessions', :action => 'new'} %> </li>
<% end %>

I am using the wicked gem which also has the following steps:

include Wicked::Wizard
steps :business, :login, :payment

If a user enters the form_for values for new method in users_controller and submits it, the user goes to the next step but the link it shows above is "Logout" ie the user is logged in before signup.

What to do?

Pls, any solution given is appreciated

users_controller.rb:

 def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      @user.update_attributes(user_params )
      session[:user_id]= @user.id
      redirect_to user_steps_path
    else
      render :new
    end
  end

  private
  def user_params
    params.require(:user).permit( :fname, :lname, :email, :mob, :gender, :country, :state, :suburb, :postal ,:add)
  end
end

user_steps_controller.rb

include Wicked::Wizard
  steps :business, :login, :payment

  def show
    @user = current_user
    render_wizard
  end

  def update
    @user = current_user
    params[:user][:current_step] = step
    session[:user_id]= @user.id
    @user.update_attributes(user_params )
    render_wizard @user
  end

  private
  def redirect_to_finish_wizard(options = nil)
    redirect_to root_url
  end

  def user_params
    params.require(:user).permit( :current_step,:cmpyname, :abnacn, :cmpyadd, :cmpydet,:cash, :paypal,:bsb,:usrname,:password, :password_confirmation, :selcat, :protit, :prodes)
  end
end

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception
  helper_method :current_user


  private
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

end

You need to sign out the user after creating it, you can do something like this

if resource.save
  sign_out resource # resource = @user

You might need to override devise registrations controller for that if you are using devise!

EDIT:

In your create action you are setting session for newly created user, remove this line from your create action

 session[:user_id]= @user.id

Hope this helps!

Instead of checking with current_user you should check <% if session[:user_id].present? %> <% if session[:user_id].present? %>

It may solve your problem

Just check in your views if the user is logged in to show your step form:

<% if user_signed_in?%>
instead of 
<% if current_user%>

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