Render partial on button click - ruby-on-rails

I'm having trouble rendering a particle on a button click. I'm getting the error
HelpController#FAQ_help is missing a template for this request format
and variant. request.formats: ["text/html"] request.variant: []
main_page.html.erb
<%= link_to "FAQ", FAQ_help_path %>
<div id="content">
</div>
help_controller.rb
def FAQ_help
respond_to do |format|
format.html {}
format.js
end
end
FAQ_help.js.erb
$('#content').html("<%= render :partial => 'FAQ_help' %>");
_FAQ_help.html.erb
<div>
<h1> This is the FAQ </h1>
</div>
routes.rb
get 'FAQ_help', to: 'help#FAQ_help'

How about this:
# main_page.html.erb
<%= link_to "FAQ", FAQ_help_path, remote: true %>
And
# faq_help.js.erb
$('#content').html("<%= escape_javascript(render :partial => 'FAQ_help')%>")
#fixed syntax
Cheers!

Since you have created a method for this request I would define it a little more for clarity:
def FAQ_help
respond_to do |format|
format.html {}
format.js {render: FAQ_help}
end
end
If you want to keep your controllers clean I would pass a param with the link and check if the pram is present if it is render FAQ partial:
View:
<link_to "Your FAQ" your_path, data: {method: :get, :remote => true, params: { faq: "#{current.user_name}" }.to_param }, :class =>"button" %>
Controller:
if params[:faq].present?
respond_to do |format|
format.html {}
format.js {render: FAQ_help}
end
else
respond_to do |format|
format.html {}
format.js {} #your default view for this method
end
end
This is handy to display IE:different content as per the request
ie: parms =bob # displays bobs FAQ
ie: parms =ted # displays teds FAQ
This technique is similar to what you would implement to create a search.

Related

show record on wicked_pdf on ruby on rails 5?

So i wanted show on pdf file just selected record. Now i get all record, or just first one when i search for localhost:3000/service_orders.pdf. When click the button "print" when they just give error something link this.
ServiceOrdersController#show is missing a template for this request format and variant.
My file.pdf.erb :
<% #service_order ||= ServiceOrder.find(params[:id]) do |service_order| %>
<td> <%= service_order.number %> </td> ...
%< end %>
My service orders controller :
def index
#service_orders = ServiceOrder::Collector.call(params: params.permit!.to_h)
respond_to do |format|
format.html
format.json
format.pdf {render :pdf => "service_orders/file",
:template => '/file', formats: :html, encoding: 'utf8'}
end
end
...
...
def show
#service_order = ServiceOrder.find(params[:id])
end
My link button is on show.html.erb
<%= link_to 'Print', service_order_path(#service_order, :format => :pdf),:format => :pdf, class:"button-show "%>
And of course my mimi_types.rb file include Mime::Type.register "application/pdf", :pdf
I been looking for internet, and closest thing was this. I hope someone was the same problem.
You do not specify format in show action so by default it tries to render html template. You have to add:
def show
#service_order = ServiceOrder.find(params[:id])
respond_to do |format|
format.html
format.json
format.pdf do
render pdf: "name_of_template",
some other options
end
end
end
And locate template for this action in views/service_orders/show.pdf.erb

Rails: AJAX + destroy action in controller not working?

Hi I am currently making an app with users, albums, and photos. I managed to make the AJAX work for the create action when I upload pics, but I can't get AJAX to work for the delete. Instead, it redirects me to an empty show page for the pic. Can anyone give me some guidance?
photos controller:
def destroy
#user = User.find(params[:user_id])
#album = #user.albums.find(params[:album_id])
#photo = #album.photos.find(params[:id])
# #photo.destroy
flash[:success] = "Photo successfully deleted."
respond_to do |format|
if #photo.destroy
format.js
# format.json { render json: #photo, status: :created, location: #photo}
# redirect_to user_album_path(#user, #album)
end
end
end
destroy.js.erb (I'm supposed to have this right?)
$('.destroypicswrapper').remove(".<%= #photo.avatar.url %>");
_photodestroy.html.erb partial:
<div class="picthumb <%= photo.avatar.url %>">
<%= link_to image_tag(photo.avatar.url ? photo.avatar.url(:small) : #album.name), root_url %>
<br>
<%= link_to "Delete", user_album_photo_path(#user, #album, photo), :remote => true, method: :delete, data: { confirm: "Are you sure? "} %>
</div>
and finally the albums/edit.html.erb page where the delete is initially happening:
<% provide(:title, "Edit album") %>
<%= render 'form' %>
<div class="destroypicswrapper">
<% if #album.photos.any? %>
<%= render :partial => 'photodestroy', :collection => #album.photos, :as => :photo %>
<% end %>
</div>
I don't think you want to render the full url of an image as a class of a div. Also, your Jquery is wrong. Try something like this:
destroy.js.erb
$('.destroypicswrapper > #photo_<%= #photo.id %>').remove();
Adjust your _photodestroy.html.erb to:
<div class="picthumb" id="photo_<%= photo.id %>">

Kaminari ajax, it does NOT works, rails 3.1 and Mongoid

I'm following https://github.com/amatsuda/kaminari_example/commits/ajax. I'm using kaminari 0.12.4. (I had check with 0.13.0 and and does not work). Without ajax does works fine.
I have in users_helper.rb
module UsersHelper
def link_to_next_page(scope, name, options = {}, &block)
param_name = options.delete(:param_name) || Kaminari.config.param_name
link_to_unless scope.last_page?, name, {param_name => (scope.current_page + 1)}, options.merge(:rel => 'next') do
block.call if block
end
end
end
I have in my users_controller the next method:
def followers
#user = User.find(params[:user])
#followers = #user.followers_by_type("user")
#followers = Kaminari.paginate_array(#followers).page(params[:page]).per(1)
respond_to do |format|
format.js
format.html { render :layout => 'panel'}# followers.html.erb
format.json { render json: #followers }
end
I have in my follower.index.html.erb the next:
<%= render 'followers' %>
<div id="paginator">
<%= link_to_next_page #followers, 'Next Page', :remote => true, :url => { :controller => "users", :action => "followers"} do %>
<span>No More Pages</span>
<% end %>
</div>
I have in partial _followers.html.erb
<% for followers in #followers%>
<div class="followers">
<%= followers.username %>
</div>
<% end %>
I have in followers.js.erb
//$('#box').html('<%= escape_javascript render(:partial => 'followers') %>');
//$('#box #paginator').html('<%= escape_javascript(paginate(#followers, :remote => true).to_s) %>');
alert ('I am follower.js.erb file');
I have to see a one alert but not seen. Where is the alert? :O. The file index.js.erb does not gets nothing. Where is the problem?
In my log appear working fine the js.erb:
Started GET "/maserranocaceres/followers?page=2" for 127.0.0.1 at 2012-01-31 05:26:44 -0800
Processing by UsersController#followers as JS
Parameters: {"page"=>"2", "user_id"=>"maserranocaceres"}
Rendered users/_followers.html.erb (1.1ms)
Rendered users/followers.html.erb (28.7ms)
Completed 200 OK in 38ms (Views: 34.5ms | Solr: 0.0ms)
it might sound a silly suggestion, but i have had issues like this in the past where changing the order of the formats helps, try:
respond_to do |format|
format.html { render :layout => 'panel'}# followers.html.erb
format.json { render json: #followers }
format.js
end

How to return a the index.html.erb in the js.html.erb file

I have a controller like so:
def index
respond_to do |format|
format.html {}
format.js {render :layout => false}
end
end
index.html.erb
hello i"m index.html.erb
index.js.erb
<%= render :template => "dashboard/index" %>
What I want to do is have index.html or index.js return the same thing. But I can't see to do that without making index.html.erb a partial (_index.html.erb) but then when a user makes an html call that won't work.
How can I get the index.js.erb to render index.html.erb? which I would then escape with jQuery. Thanks
Try:
<%= raw render(:template => "dashboard/index").to_json %>
I have this code in my application to do it:
<%= escape_javascript(render :file => "/posts/index.html.erb") %>
No sure if it's a "right" way, but it definitely works.

Adding my comments to the index view...Ruby on Rails

Ok...I am new to rails so this may be a stupid question but need help. I just watched and implemented Ryan Bates screencaset about setting up a blog. You can see it here http://media.rubyonrails.org/video/rails_blog_2.mov. Here is the skinny:
Two tables: Posts and Comments. Everything works great including the addition of comments via AJAX. The default development of this blog gives you the index.html.erb view where you can view all the posts
def index
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
format.json { render :json => #posts }
format.atom
end
end
The comments are only viewed via the show.html.erb page and is displayed via this code in that file:
<%= render :partial => #post %>
<p>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%= link_to 'Destroy', #post, :method => :delete, :confirm => "Are You Sure" %> |
<%= link_to 'See All Posts', posts_path %>
</p>
<h2>Comments</h2>
<div id="comments">
<%= render :partial => #post.comments %>
</div>
<% remote_form_for [#post, Comment.new] do |f| %>
<p>
<%= f.label :body, "New Comment" %><br/>
<%= f.text_area :body %>
</p>
<p><%= f.submit "Add Comment"%></p>
<% end %>
What I am trying to do is to get similair representation of the comments functionality to exist in the index.html.erb view (which I will hide with javascript). Which currently just looks like this:
<h1>Listing posts</h1>
<%= render :partial => #posts %>
<%= link_to 'New post', new_post_path %>
My initial thought was just to put this exact same code that is in the show.html.erb file in the index.html.erb file but that doesn't work. I have tried a bunch of things here but I am not familiar enough with Rails (or coding for that matter) yet to do this in a timely manner. I get two main errors. Either that I passed a nil.comments error or an undefined object/method (can't remember).
My question is what do I need to included in the post_controller, the comments_controller and the index.html.erb file to accomplish this. To be complete I have included the code in each below.
POSTS_CONTROLLER
class PostsController < ApplicationController
before_filter :authenticate, :except => [:index, :show]
# GET /posts
# GET /posts.xml
def index
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
format.json { render :json => #posts }
format.atom
end
end
# GET /posts/1
# GET /posts/1.xml
def show
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
# GET /posts/new
# GET /posts/new.xml
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
# POST /posts
# POST /posts.xml
def create
#post = Post.new(params[:post])
respond_to do |format|
if #post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(#post) }
format.xml { render :xml => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.xml
def update
#post = Post.find(params[:id])
respond_to do |format|
if #post.update_attributes(params[:post])
flash[:notice] = 'Post was successfully updated.'
format.html { redirect_to(#post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
private
def authenticate
authenticate_or_request_with_http_basic do |name, password|
name == "admin" && password == "secret"
end
end
end
COMMENTS_CONTROLLER
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create!(params[:comment])
respond_to do |format|
format.html { redirect_to #post}
format.js
end
end
end
INDEX.HTML.ERB
<h1>Listing posts</h1>
<%= render :partial => #posts %>
<%= link_to 'New post', new_post_path %>
Here's one simple solution. Step 1, edit your index action to include all the comments belonging to each post.
def index
#posts = Post.all(:include => :comments)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
format.json { render :json => #posts }
format.atom
end
end
Step 2, edit your index view to display each post, followed by its comments:
<h1>Listing posts</h1>
<% #posts.each do |post| %>
<%= render :partial => post %>
<%= render :partial => post.comments %>
<% end %>
<%= link_to 'New post', new_post_path %>
Edit: I'm not 100% sure of the best way to include the comment creation form also. What I'd try first is this (in index.html.erb):
Try changing your index view to:
<h1>Listing posts</h1>
<% #posts.each do |post| %>
<%= render :partial => post %>
<%= render :partial => post.comments %>
<% remote_form_for [post, Comment.new] do |f| %>
<p>
<%= f.label :body, "New Comment" %><br/>
<%= f.text_area :body %>
</p>
<p><%= f.submit "Add Comment"%></p>
<% end %>
<% end %>
<%= link_to 'New post', new_post_path %>
This should render the "New comment" form for a given post under the comments for that post, but I'm not sure (without actually trying it out) whether the form submission AJAX will successfully update and refresh the index page.

Resources