pratik mokal
pratik mokal

Reputation: 21

How I Pass parameters from View to Controller In Ruby

#app/controllers/sessions_controller.rb
class SessionController < ApplicationController
  def new
    @session = Session.new 
  end

  def fetch
    #@user = User.session(params [:user])
    redirect_to "http://www.google.com"
  end

  def create
    emai = params[:email]
    puts emai
    user = User.find_by(:email => session[:emai])

    #user = User.find_by (params [:email])
    #user = User.find_by email: '[email protected]'
    #user = User.find_by(params[:Email])

    #if (session[:Email] = user.email)
    if (user)
    redirect_to "http://www.yahoo.com"
      flash[:notice] = "You signed up successfully"
      flash[:color]= "valid"

    else
      flash[:notice] = "Form is invalid"
      flash[:color]= "invalid"
      redirect_to "http://www.google.com"
    end
    #redirect_to "http://www.yahoo.com"
  end
end

every time i execute my view i get redirected to google.com even though i pass the parameters.


Edit by R Peck:

My logic should send people to Yahoo if the params are set, but still sends to Google, how can I fix this?

Upvotes: 0

Views: 168

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Several things wrong with your code.

Here's what I'd write:

#app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
   def new
      @session = Session.new
   end

   def create
      email = params[:sessions][:email]
      user = User.find_by email: email

      url    = user ? "google" : "yahoo"
      colour = user ? "valid" : "invalid"
      notice  = user ? "You signed up successfully" : "Your form is invalid"

     redirect_to "http://#{url}.com", notice: notice, color: colour
   end

   private

   def session_params
      params.require(:session).permit(:session, :params)
   end
end

OOP

I think this may be a little advanced but I'll write it anyway, for my own benefit.

Rails is object orientated (it's built on Ruby which is an OOP language). This means that each time you create/call a controller, it should be centered around objects.

A good example for you would be the Devise controllers.

This has a sessions_controller which essentially allows you to CRUD (Create Read Update Destroy) a session. This is the correct way to use a controller.

Your implementation seems to be dealing with a user, rather than a session, and as such you'd be best using a users_controller to fix it:

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def new
      @user = User.new
   end

   def create
      @user = User.new
      @user.save
   end
end

Having said that, it does seem that you're probably going to resolve the issue to make it so that you can use the User to build a new session.

I guess it's best to remember that you have to ensure you're able to appreciate a good structure for your application

Upvotes: 0

user5245636
user5245636

Reputation:

Try:

 user = User.find_by(:email => params[:sessions][:emai])

You are not getting the value of email if you only call params[:email] you should call parent first before calling the child params[:sessions][:email].

Upvotes: 1

Related Questions