Rails: How to render partial view from another controller - ruby-on-rails

I am making a reddit clone. Right now I have a voting system that works, but it refreshes the page each time a user votes. I want to make this async by using AJAX.
Making buttons respond to Jquery is really hard though because they change state depending on how a user has voted on a post. The solution I came up with is this: Have 2 upvote buttons in the _upvote partial, and have 2 downvote buttons in the _downvote partial. Each partial will have one voting button that is activated when the case is already true, and clicking on it again will destroy it (for example, if a post is already upvoted on, then reclicking the upvote button will destroy it). The other button(s) in the partial will be activated in the case that the button has not been used yet (in which case clicking the button will create it).
Example:
****The _upvote partial should have the following buttons in it:****
1)create_upvote
2)destroy_upvote
****The downvote partial should have the following buttons in it:****
1)create_downvote
2)destroy_downvote
Whichever buttons are activated will depend on the controller.
My question(s) are this:
1) What is the actual syntax to a render partial view from another controller? For example: posts is rendering _post, and within that I want _upvote or _downvote to be rendered. How to render _upvote or _downvote from the controller. Again, the reason I WANT to render these partials from a controller is because the controller will be changing their state.
Downvotes partial
app/views/posts/_downvote.html.erb
Upvotes partial
app/views/posts/_downvote.html.erb
Posts partial
app/views/posts/_post.html.erb
Downvotes controller
Upvotes controller
Line 9 in the controllers is where my syntax is off! What is the syntax to render _upvote and _downvote partials, within the _posts partial? Thanks mates!

As far as syntax goes, what you want is the following:
render partial: "posts/upvote"
Instead of sending html data (the buttons you mentioned) from controller to the partial, you should have a conditional statement in each partial that activates one button or another. From there create event handlers for each respective button. That takes care of the issue of Jquery handling internal state.

Just a small note for the special cast of partials inside views/layouts/_upvote.html.erb, you should use
render partial: "layouts/upvote"
(not application/upvote)

Related

Both create and list in the same view MVC4?

I am new to MVC4. I know basic CRUD functionality in MVC, but how can I achieve both create and list in the same view? Like after a user creates data the user will automatically be redirected to the same view and view the list of data.
You may use partial views to accomplish this. Create the create / edit pages the same way as you do and below the form you may render a partial view that would render the list with even edit / delete buttons.
Alternatively, you may pass both the form data (in case of edit) and the list data to a view to render there.
I would suggest you to use the partial views approach as it would simplify the view and the partial view can be reused on some other page also.
Please take this as a starting point and not as a follow-it-blindly solution.

Show different partial in Controller#show

I have 3 different partials, each representing a different step of this process: "_overview.slim", "_setup.slim", and "_submit.slim". I want to show these 3 different partials all in "show.slim" only one at a time, and one after another as the user clicks on "Go to next step". How can I accomplish this in the show action of my controller?
Let say in your show,have a 'Next' button to trigger the next partial.
In the 'Next' button, I can pass a param[:page1] and all the necessary param when click.
In the 'Show', if there is param[:page1] ,then display _partial1.html.erb
its goes all the same.
So if I were you, I would put render all three partials in your show view, and use Javascript, and CSS to manage when each one was shown. If you drop each of those partials in a div, its easy to use JQuery's hide and show methods, linked to the click event of the "Go To Next Step" button. To do it this way you wouldn't have to touch your Controller at all.

Rails - How do I create a checkbox that will render a different form when checked without refreshing?

As the title suggests, I am wondering how to create a checkbox on a view that will change what form is rendered without refreshing the page.
In my app/views/home/ folder I have _form_1.html.erb and _form_2.html.erb.
I want to default to <%= render 'form_1' %> when the box is not checked, and change to <%= render 'form_2' %> when the box is checked.
I've attempted to look up how to do this, but things like .is(":checked") and check_box_tag don't seem to be working for me, or I'm using them wrong.
Any help is appreciated!
EDIT: I am using Rails 3.2.12 and Ruby 1.9.3.
The way I'd do this is to render both the form partials from another view, and put each of them in a div. The default form div should be visible, and the optional form div should be hidden. Then you can toggle the display of the divs with a click action on the checkbox.
Depending on what's in the forms and how they interact with your controller, there's likely a lot of other work involved in processing, but in order to toggle the displayed forms, the method above is one option.
If you want to do this without a refresh you'll have to use javascript. What you'd do is render each form but as hidden. This next part is really easy with jquery. When a checkobox is selected you'd just do something like $('#form1').show(), and conversely $('#form2').hide()

Rendering controller/action within a different view

I am looking for a way in Ruby on Rails to render a completely different controller's action within other views.
For example I am writing a band website that displays their albums. So I have /albums/list that shows a list of all of their albums with other stuff around the page as the layout. Now what I am looking to do is also render /news/list in every page as part of the layout as well so that every page you go to you can see it.
I cannot find a way to call the news controller list action and display it in the list view using partial views. Any help would be appreciated.
http://api.rubyonrails.org/classes/ActionView/Helpers/RenderingHelper.html#method-i-render
render() method give you ability to render specific file, placed in any directory :)
also read this article - http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2 especially about "Custom partial paths".

Change partial view on link press

I have something like
#{ Html.RenderPartial(#"~/Views/Management/_Main.cshtml"); }
Can I change this view without page refreshing? I mean, I want to render few partials on one place. Like gallery, may be. I mean, I press the link - some section loaded, depending on argument.
First I think about - master page (Layout page). Any other ideas?
You can dynamically load a partial view into a div without a page reload by using the #Ajax.ActionLink() method.
Have a look at this post.

Resources