0

I am trying to set the instance variable from the controller from the view. For example:

class UsersController

def new
  @admincheck = false
end

in view: home.html.erb

<%= link_to "Sign up", signup_path, @admincheck => true, :class => "signup_button round" %>

with setting @admincheck to true in the view, will the UsersController respond to that by receiving @admincheck that is true?

I am unsure whether you can assign instance variables values in the view for the controller to use. Thanks

4 Answers 4

1

You cant set the instance variable in view for controller. Instead of doing this you can pass a parameter from view to controller. I think you want to add a check that only admin should be able to 'Sign Up'. For this you can check the current user status in the controller and proceed.

Sign up to request clarification or add additional context in comments.

1 Comment

I actually want both admin and regular users to sign up using the same form. I just want to pass in something to the controller to let it know that this person is trying to sign up as an admin or a user so that it can record in the database accordingly. Thanks.
1

Try to use Filters to handle those kind of things. It shouldn't be the responsibility of the view to handle the authentication decision for the next request.

What do you want to do exactly?

2 Comments

I have a Users table that has boolean admin column. I want both regular users and admin users to sign up using the same form. So, I have two different sign up buttons leading to the same sign up form, but I was trying to set @admincheck instance variable as true through the form to let the controller know that this person wants to be an admin so set admin to be true in the user table for special privileges. Thanks
I'd suggest to use different resources for user and your admin user. Take a look at namespaces in rails routing system. With namespaces you could have the routes /users/new and /admin/users/new with the corresponding controllers users_controller and admin/users_controller. You'd gain a clean separation. DRY your code with shared methods and views.
1

You should simply do something like this:

<%= link_to "Sign up", signup_path(:admincheck => true), :class => "signup_button round" %>

Then in your controller you can get admincheck as @admincheck = params[:admincheck]

1 Comment

then does @admincheck == true? ive tried this and it doesn't seem to work this way
1

You can't set a instance variable from the view for the controller. Think of with respect to request response cycle:

Browser -> Request -> Controller -> View -> Response -> Browser

You want to pass something from view to controller and as view is down the line in the above illustration it can't pass a variable to controller, instead you will need to pass the data as form field and capture the same in the controller as already suggested by Pravin and Ashihsh.

1 Comment

This is a clear and simple explanation of why you can't do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.