yellowreign
yellowreign

Reputation: 3638

How to Check the URL or path in Rails?

I have a Rails app where I have a show action with a form. This show action uses a view that checks if user_type == "admin" and displays a partial accordingly. For this show view, only if user_type = admin can access it.

Once the form is submitted, it triggers another action in the same controller (store). The issue I'm having is that when there is an error with the submission, store renders show without reference to the user, so the partial doesn't appear since user_type won't equal admin (since it can't find the user). However, since access to 'show' is protected, I'm wondering if there's a way I can check the url.

Example: the form is on:

http://foo.com/bars/4/users/3

however, when it fails, the render url becomes:

http://foo.com/bars/4/users/store

the show.html.erb view has this:

<%= form_for([:bar,@user], :url => store_bar_users_path(params[:bar_id]), :html => {:name=>"users_form",:multipart => true,:class=> "standardForm"}) do |user| %>

<%= render :partial => "users/admin_options" if @user.user_type == "admin" %>

in the store action in the controller:

 if [email protected]
  render :action  => "show" ,:layout => 'application'
 else

So what I'm wondering is if I can change the conditional in:

<%= render :partial => "users/admin_options" if @user.user_type == "admin" %> 

to somehow also also check the url for store_bar_users_path, or something like that - or perhaps the render in the store action is incorrect.

Upvotes: 2

Views: 429

Answers (1)

Anil
Anil

Reputation: 3919

Store the user type in a session variable, set when the user logs in. Then you don't have to keep loading it, and it is visible to all controllers, views, and helpers. Use this variable to determine if you want to show the partial.

Upvotes: 1

Related Questions