Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9135901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:52:57+00:00 2026-06-17T08:52:57+00:00

Rails form validation is designed to go in the model most easily. But I

  • 0

Rails form validation is designed to go in the model most easily. But I need to make sure the current user has the required privileges to submit a post and the current_user variable is only accessible in the controller and view.

I found this answer in a similar question:

You could define a :user_gold virtual attribute for Book, set it in the controller where you have access to current_user and then incorporate that into your Book validation.`

How can I set this up with my post and user controller so that the current_user variable is accessible in the model?

Solution:

This whole thing is wrong from an application design perspective as @Deefour’s answer pointed out. I changed it so my view doesn’t render the form unless the condition is true.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:52:58+00:00Added an answer on June 17, 2026 at 8:52 am

    The “similar question” is saying you can do something like this

    class YourModel < ActiveRecord::Base
      attr_accessor :current_user
    
      # ...
    end
    

    and then in your controller action you can do something like

    @your_model = YourModel.find(params[:id])
    @your_model.current_user = current_user
    @your_model.assign_attributes(params[:your_model])
    
    if @your_model.valid?
      # ...
    

    You can then use self.current_user within YourModel‘s validation methods.


    Note I don’t think this is what you should be doing though, as I don’t consider this “validation” as much as “authorization”. An unauthorized user shouldn’t even be able to get the part of your action where such an update to a YourModel instance could be saved.


    As for doing the authorization with Pundit as requested, you’d have a file in app/policies/your_model.rb

    class YourModelPolicy < Struct.new(:user, :your_model)
      def update?
        user.some_privilege == true # change this to suit your needs, checking the "required privileges" you mention
      end
    end
    

    Include Pundit in your ApplicationController

    class ApplicationController < ActionController::Base
      include Pundit
      # ...
    end
    

    Then, in your controller action you can do simply

    def update
      @your_model = YourModel.find(params[:id])
      authorize @your_model
    
      # ...
    

    The authorize method will call YourModelPolicy‘s update? method (it calls the method matching your action + ? by default) and if a falsy value is returned a 403 error will result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a Rails form that responds back to the user live
Am pretty new to ROR. Need help in Rails form validation. Am using rails
There are two places to put form validation when developing with rails - Model
I am using client_side_validations gem to perform a form validation in rails 3. everything
I was wondering how to go about form validation in rails. Specifically, here is
I have a form, generated through rails helpers, that has some AngularJS on it.
How to use jquery validation plugin for the rails form given below. I want
I have a registration form where user has to enter his credentials plus a
Forms offer no differentiation value for your service, but most web sites need them
I'm having some issues using rails validation on a form. What I'm doing is,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.