0

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?

3
  • Usually you modify the emitted HTML to use different styles rather than changing the CSS. Are you using jQuery? Commented Nov 17, 2014 at 21:14
  • Even if you do the actual logic on the controller, you'll need to either wire up a submit when the user makes a selection or you'll need to use javascript to send the selection across the wire. Basically, if there's a way to do this, it's well beyond me. Commented Nov 17, 2014 at 21:30
  • No, tadman, just straight Ruby, HTML, and CSS. All I want to do is change the properties of the tables as needed (visible or hidden) using whatever selection they chose when they submitted the form. Commented Nov 18, 2014 at 1:44

1 Answer 1

1

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.

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

1 Comment

This worked perfectly, @johndavid400, and it was so simple for me to implement. I know SO doesn't like comments used to say thanks, but in this case, a thanks is in order!

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.