Reloading rails view from another controller - ruby-on-rails

Suppose I have the following view in my rails app
<% if is_subscriber? %>
<%= render :partial => 'subscriber_page' %>
<% else %>
<%= render :partial => 'payment_form' %>
<% end %>
The payment form submits a post-rquest to the controller show page. In my routes file I have:
match ':users(/:id)', :to => 'users#submit_payment', :via => :post
My submit payment helper method is:
def submit_payment
current_user.is_subscriber = true
show
end
However when I run this I get a submit_payment template missing error. How can I make sure that the users show page is simply reloaded when the helper method is finished.

It looks like you're trying to redirect to the show page. Try this:
redirect_to user_path(#user) # Note you need to send the show page an :id param
If you're just wanting to render a specific partial, do this:
render :partial => 'show'
However, if you want something more seamless, like submitting the form VIA Ajax, which gets back a partial and then on success dumps it in to a waiting DIV or something, you can do that as well, though with a bit more complexity.

Related

Render remote partial via controller on load

How do I render a partial view via its controller when loading a separate view in Ruby on Rails?
Thanks for your help in advance
Problem: I have a show.html.erb for my Patients controller, when this loads I have a series of tabs, one of which is a list of notes added to that patient record. Notes is a separate model. I want to load the list of notes when loading show.html.erb.
I can use the code below to render the view directly and can pass variables in this way, but I would rather go through the controller and deliver notes/_list.js.erb, via the Patients Controller.
<%= render "notes/list", :remote => true %>
I have a solution that loads the partial via a link_to but because the link is in a tab it then breaks my tab javascript, and would rather just load the information on initial page load rather than have the user interact with an element.
<%= link_to "Notes".html_safe,{:controller => "patients", :action => "notes", :remote => true } %>
It needs to be in a partial as I am then using pagy to paginate the results and if I display it via a controller dependent partial then the pagy navigation links function as expected.
Relevant Routes
get 'patients/:id/show' => 'patients#show', as: :show_patient
get 'patients/:id/notes' => 'patients#notes', as: :notes_patient
Relevant Patients Controller
def notes
#pagy, #notes = pagy( Note.where(:patient_id => params[:id]).reverse_order, items: 2 )
respond_to do |format|
format.html { render "notes/list" }
format.js { render "notes/list" }
end
end
notes/list.js.erb
$('#notes-list').html('<%= escape_javascript(render('notes/list')) %>');
notes/_list.html.erb
<% #notes.each do |note| %>
<%= render partial: note %>
<% end %>
<%== pagy_materialize_nav(#pagy) %>
<span class="pagy-info"><%== pagy_info(#pagy) %></span>
I believe you can do something like this in the controller:
# or whatever this partial is called
#notes_rendered = render_to_string "notes/list"
And then use it in the view you want.

How to render show action as a partial

I have a search form. when i search for a record, the result goes to the show page as expected , but i want the result to render on the same page where ever i put the form partial
I want to create a partial that will render the show action on the same page with the form
Here is the shipment controller
class shipmentsController < ApplicationController
def show
#shipment = Shipment.find_by_trackCode(params["trackCode"])
unless #shipment.present?
end
......
Home controller index.html.erb
<%=render 'shipments/form' %>
Views/shipments/_form.html.erb
<%= form_tag search_path, :controller => 'shipments', :action => 'show', :method => 'get' do %>
<%= text_field_tag :trackCode %>
<%= submit_tag 'Track' %>
<% end %>
The present code will dispaly the result on the show page but I want the search result to be rendered using a partial so that i can render it where ever i use the form partial.
How can i archive that ?
are you really sure it's a good idea to use your show view as a partial? now if you are... you could rename your file _show.html.erb and reference it passing the folder name <%=render 'shipments/show' %> but you'd also have to change the loop to call the model Shipment.all.each instead of #shipment
render template: 'shipments/show', locals: {shipment: #shipment}
use variable shipment instead of #shipment in show.html.haml

Ruby on Rails - redirect_to

I'm having some trouble understanding the redirect_to statement.
I have a model "Book" (with an boolean attribute "read")and a controller "books". Now I created a second controller "Admins" having to methods: index and change.
The index view just renders a list off all Books with a link to the change method:
<% #Books.each do |d| %>
<%= d.title %><br>
<% if d.read==true %>
<%= link_to "mark unread", change_path(:id=>d.id)%>
<% else %>
<%= link_to "mark read", change_path(:id=>d.id)%>
<%end %>
Now the change method just changes the "read" attribute:
#book=Book.find(params[:id])
if #book.read==true
#book.update_attributes(:read => false)
else
#book.update_attributes(:read => true)
end
redirect_to action: "index"
The Problem is: rails tries to redirect me to the show action using the :id as a parameter...(perhaps because the change_url is /admins/change?id=3)
But I just want to be directed back to the index view "/admins"
is there a way? it seems like rails always tries to redirect to the view action if there is an id as a parameter
Thanks alot
PS: the routes.rb contains resources:admins and resources:books
Use this
redirect_to :controller => 'admins', :action => 'index'
Or
redirect_to admins_url
The above two will direct you to the index page of AdminsController. There is no way that Rails would route it to show action UNLESS you are redirecting to show action from the index action of AdminsController. Since, you did not share index action code of AdminsController, I would recommend you to check there.
If you want a clear explanation of redirect_to ... checkout
https://gist.github.com/jcasimir/1210155
I had a kind of similar issue some days ago. I would suggest to do this within the form where you list the books and the mark/unmark checkboxes.
<%= form_for #book,:url => book_index_path do |f| %>
This worked fine for me, when I set up a site where you create data and the user is immediately redirected to the same page (incl. success/error message).. to do a kind of
human batch-processing.

Deleting records using forms in rails 3.2

I have two models, users and materials. Users can favourite materials. I have set up the relationships and the code for favouriting works fine but I can't seem to get the code for unfavouriting right. I have the following code for unfavouriting:
Materials Controller (in show action where unfavourite form is)
#favourite = Favmat.where(:user_id => current_user.id, :material_id => #material.id)
Note: I use this code to decide which button to show in the view. Assuming a record exists we get this:
View
<%= form_for #favourite, :method => :delete do |f| %>
<%= f.submit "Unfavourite" %>
<% end %>
The problem seems to be here. Nothing I do seems to get me a working route to the destroy action in the favmats controller. I have tried using a form_tag instead but then I get very odd routes that don't work.
Favmats Controller
def destroy
Favmat.find(params[:id]).destroy
respond_to do |format|
format.html { redirect_to #material }
format.js
end
end
Update
I have also tried using link_to instead of a form. The code is as follows:
<%= link_to "Unfavourite", favmat_path, method: "delete" %>
The weird thing is that the html for this takes the favmat id from the material, not the favmat object. I don't know how to get the favmat object id in there. Nothing seems to work.
Try passing #favourite object instead of favmat_path to link_to:
<%= link_to "Unfavourite", #favourite, method: :delete %>

How do I use link_to_remote to pass then render a partial in rails?

I want to create several link_to_remote links that are campaign names:
<% #campaigns.each do |campaign| %>
<!--link_to_remote(name, options = {}, html_options = nil)-->
<%= link_to_remote(campaign, :update => "campaign_todo",
:url => %>
<% end %>
I want the output to update on the page to render a partial, which runs a loop through the values associated with the campaign.
The API docs says this will render a partial, but I'm not clear where the name of the :partial template is passed in, either here or in the controller
Thanks.
The controller would render the partial, according to the docs.
Usually, the result would be a partial prepared by the controller with render :partial.
of course in the controller. Here you create an AJAX snippet that will pull an url you specify from your controller.
generated javascript has no access to any partials b/c it runs on client PC.
and controller decides WHAT to respond to this request. it can render a partial, a template, a text, or anything.

Resources