L. Becker
L. Becker

Reputation: 59

Can you change CSS for view in Controller in Rails?

I have a view (let's call it View #1) that allows the user to select a certain table to view using a dropdown menu on a form. That parameter is then passed to my controller. What I want to do is get my controller to take that parameter and use it to change the visibility of elements on the view it directs to (View #2).

View #2 has 3 tables. Only one table should be visible at any given time once my method in my Controller runs. Currently, all 3 tables have unique IDs and they have a display:none attribute in the CSS file. So, if the user selects "1" from View #1, I want View #2 to set table 1's display to block and table 2 and 3 to display:none. Can I do this in the Controller or am I required to do this in JavaScript? If I have to do this in JavaScript, how do I pass the parameter value from the controller to the JS function call?

Upvotes: 0

Views: 848

Answers (1)

johndavid400
johndavid400

Reputation: 1582

you should probably break out each table into its own partial and only render the one you want to display:

def some_action
  @table_to_show = "table_1"
  # table_1 should be determined by the user's selection from View #1
  # probably something like: @table_to_show = params[:user_selection]
end

and in your view #2:

<%= render @table_to_show %>

which will in turn render the template: _table_1.html.erb

You can use CSS/js, but probably shouldn't for something that is handled at the controller level.

Upvotes: 1

Related Questions