i've created a JS grid and i'm trying to pull the logo image and display in div data-thumbnail
How do i add the link to pull post images?
<%- model_class = Post -%>
<div class="spacer spacer-big"></div>
<div class="container" style="padding-right:50px;padding-left:50px;">
<div id="grid">
<div class="box">
<div data-thumbnail= "TRYING TO GET POST LOGO HERE" </div>
I inserted this <%=image_tag post.logoimage.url %> between the quotation marks but i'm still getting an error.
Fix
You'll need to do this:
<%= content_tag :div, "", data: { thumbnail: #post.logoimage.url } %>
Here's info on content_tag for you
This should be accompanied with the following controller action:
#app/controllers/your_controller.rb
Class YourController < ApplicationController
def action
#post = Post.first #-> sets the post object
end
end
--
Data
The error you receive is as follows:
undefined local variable or method `post'
This means your view is calling a non-existent object (variable) (post). The solution to this is to make sure that post is set correctly. And to do this, you need to either use the controller (as per Rails' MVC pattern, or set it in the view:
<% post = Post.first %>
<%= content_tag :div, "", data: { thumbnail: #post.logoimage.url } %>
The problem is that <%- model_class = Post -%> defines model_class as a class, not an object of a class. It should be like <%- model_class = Post.first -%> to get an object.
So you should define your post, that you probably meant by first line. It should be:
<% post = Post.first %>
then you can use this post:
<%=image_tag post.logoimage.url%>
So it is already a tag. You can wrap it in any other.
Related
a controller action has two sets of data (originating form the same class, but drawn from different queries
#usercontents = Usercontent.where(contenttype_id > ?, 1).page params[:page]
#points = Usercontent.where(contenttype_id = ? ', 1]).page params[:page]
The views has partials who in turn invoke their own partials
<%= render 'points' %>
and
<%= render 'contents' %>
_contents.html.erb partial
<%= paginate #usercontents, remote: true, previous_label: t('navigate.previous'), next_label: t('navigate.next'), params: {anchor: 'panel3'} %>
<div id=usercontents class='tableize'>
<%= render partial: 'usercontents/usercontent', collection: #usercontents %>
</div>
_points_html.erb partial
<%= paginate #points, remote: true, previous_label: t('navigate.previous'), next_label: t('navigate.next'), params: {anchor: 'panel2'} %>
<div id=points class='tableize'>
<%= render partial: 'point', collection: #points %>
</div>
As the view alludes to, the pagination collections are in seperate tabulations of the page.
The end result is that pages do get paginated, but the call has three problems:
Instead of having a XHR call for just the collection, all assets are being invoked
rendering is not occuring for just the partial but the whole page (including stringified javascript!)
the pagination rendering is being placed after the partial, not in substitution of. calling another page just adds the new page to the end of previous block.
What is incorrect in this setup?
I'm trying to create a notification center that have pagination(via Kaminari)
like facebook.
I want this center to show pagination from any page(this center is contained in a header file).
Now I've written code below, then it shows appropriately when I access to home.html
But its pagination doesn't work in other page (eg. views/test/test.html.erb).
Probably, ajax seems cannot read js file so if I put test.js.erb file in the views/test, it will work.
In views of operation and maintenance, that is undesirable way.
How can I read pagination by using only one js.erb file? thank you for your helping
Source code is below.
Model
app/models/item.rb
class Item < ActiveRecord::Base
paginates_per 50
end
Controller
app/controller/items_controller.rb
before_action :pagination
def pagination
#items = Item.page params[:page]
end
View
app/views/static/home.html.erb
<%= render 'static/header' %> <!-- reading partial header-->
app/views/static/_header.html.erb
<div><%= render 'application/items', items:#items %></div>
app/views/static/home.js.erb
$('#items').append("<%= escape_javascript(render '_items', object: #items) %>");
$("#more_link").replaceWith("<%= escape_javascript(link_to_next_page(#items, 'more', remote: true, id: 'more_link')) %>");
app/views/application/_items.html.erb
<div id="items">
<%= render 'items' %>
</div>
<%= link_to_next_page #items, 'more', remote: true, id: 'more_link' %>
app/views/application/__items.html.erb
<% #items.each do |item| %>
<div><%= item.name %></div>
<% end %>
I'd suggest creating a separate controller action to handle ajax requests:
class ItemsController
def paginate
end
end
then move js.erb code under views/items/paginate.js.erb.
Kaminari's link_to_next_page helper uses link_to inside so it should be possible to add controller and action params to link:
<%=
link_to_next_page #items, 'more',
controller: 'items',
action: 'paginate',
remote: true,
id: 'more_link'
%>
My issue is that I expect on calling the destroy action of my swipe controller (which does work) that it would then call the javascript in the file app/views/swipes/destroy.js.erb. The idea being that on calling this file it will refresh my my-deck div and provide an updated web page. Unfortunately, although my destroy action is working, it doesn't seem to be calling the js correctly and I have to refresh the webpage to see that the item has been deleted.
Here are the relevant files, let me know if more info could be useful.
app/controllers/swipes_controller.rb
class SwipesController < ApplicationController
def new
end
app/views/swipes/destroy.js.erb
$(".my-deck").alert("Foo");
app/views/my_deck/show.html.erb
<p> Welcome to my deck! </p>
<p> When this is working your right swiped events should appear beautifully below! </p>
<div class="my-deck">
</div>
app/views/my_deck/_my_deck.html.erb
<p class="text-center">
There are no items in your shopping cart. Please <%= link_to "go back", home_path %> and add some items to your cart.
</p>
<% end %>
"app/views/my_deck/_card_row.html.erb"
<div class="well">
<div class="row">
<div class="col-xs-8">
<h4><%= event_id %></h4>
</div>
<div class="col-xs-4">
<div class="row">
<div class="col-xs-4">
</div>
<div class="col-xs-8 text-right">
<div class="btn-group">
<%= link_to "Delete", myCard, { data: { confirm: "Are you sure you wish to delete the product '#{myCard.event_id}' from your cart?"}, method: :delete, remote: true, class: "btn btn-danger" } %>
</div>
</div>
</div>
<% end %>
</div>
</div>
</div>
Heres the output of my rails server when I click delete:
Swipe Load (0.8ms) SELECT "swipes".* FROM "swipes" WHERE "swipes"."id" = $1 LIMIT 1 [["id", 48]]
(0.1ms) BEGIN
(1.6ms) COMMIT
Rendered my_deck/_my_deck.html.erb (74.5ms)
Rendered swipes/destroy.js.erb (79.0ms)
Completed 500 Internal Server Error in 91ms (ActiveRecord: 3.0ms)
ActionView::Template::Error (undefined method `size' for nil:NilClass)
app/views/my_deck/_my_deck.html.erb:1:in `_app_views_my_deck__my_deck_html_erb___162867021113944140_70146212264540'
app/views/swipes/destroy.js.erb:1:in `_app_views_swipes_destroy_js_erb__359810691289434442_70146212224340'
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.6ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.7ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (8.9ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.6ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.5ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (14.6ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (30.9ms)
If someone could help me figure out why app/views/swipes/destroy.js.erb is not running that'd be great. I'm following this tutorial.
This happens because destroy.js.erb
$(".my-deck").alert("Foo");
$(".my-deck").html("<%= escape_javascript(render 'my_deck/my_deck') %>");
renders app/views/my_deck/_my_deck.html.erb, that, in order, executes this line:
<% if #myDeck.size == 0 %>
Since you don't define #myDeck anywhere (neither in your controller's destroy action, nor in destroy.js.erb) this variable initializes to nil, which doesn't have any .size method, that causes error you have.
To fix the error you need either to properly initialize #myDeck var or to change your template so it won't access to uninitialized vars anymore (for example, remove line $(".my-deck").html("<%= escape_javascript(render 'my_deck/my_deck') %>");).
Have a nice day!
I had a similar problem when testing some ajax events, because something was missing on my action:
Here is what I added inside my destroy action:
respond_to do |format|
format.js
end
Try change your destroy to:
def destroy
#swipe = Swipe.find(params[:id])
if #swipe.destroy
redirect_to myDeck_path
end
respond_to do |format|
format.js
end
end
I hope it works for you.
I have a list of user 'submissions' in my Rails app, and when a user submission is clicked, I would like the full submission to load into the view, without having to go to a new page.
Here's the code for the list of submissions:
<div id="submission-list-container">
<% current_user.submissions.each do |i| %>
<a href='#'>
<div id="post-container">
<%= i.title %>
</div>
</a>
<% end %>
</div>
The partial I have created, <%= render "show", :submission => i %>,, works fine, but I would like the full submission to be loaded into the view (index.html.erb), when that link above is clicked. Is there a good method for doing this? Should I just do something else like an AJAX call in JavaScript? I like these partials because it feels more clean and organized to seperate code.
My partial is pretty simple at the moment:
<%= submission.title %>
<%= dat_markdown(submission.content) %>
You can do this using a TitlePane in the Dojo Toolkit, and probably there's a similar widget with jQueryUI.
I can only speak to the Dojo Toolkit's version
You would do something like:
<div data-dojo-type="dijit/TitlePane" data-dojo-props="href: '/blah', title: '<%= submission.title %>', open: false">
You can include this in your application layout for lite usage of dojo, loaded from a CDN:
<script data-dojo-config="async: true, parseOnLoad: true"></script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojo/dojo.js"></script>
The TitlePane is wired up so that when it is expanded, it will autoload the content that is provided to the href parameter.
While this isn't exactly a rails solution directly, it can be used as an alternative.
For a Rails Solution, you can simply use an AJAX call to a controller that renders the partial. You may even be able to hook it up using the
def blah
#submission = Submission.find(...)
respond_to do |format|
format.html # default render
format.js # js behavior
end
end
then a blah.html.erb
<%= render partial: 'submission/submission', object: #submission %>
with the partial
<div id='submission-<%= submission.id %>'>
<div id='submission-<%= submission.id %>-title'><%= link_to(submission.title, 'blah/blah', remote: true) %></div>
<div id='submission-<%= submission.id %>-content'></div>
</div>
and a blah.js.erb
$.get('/submission/content', function(data) { $('#submission-<%= #submission.id %>-content').html(data) } );
and an entry into the submissions_controller with route
def content
#submission = Submission.find(...)
render text: #submission.content
end
This probably isn't an exact solution, but hopefully it'll put you on the right path.
I'm trying to render the creation of a post with jquery ajax but I can't seem to get it to work correctly. It seems to render the post with very strange (I think nonexistent) styling and the flash message appears only after the page is refreshed. The post also get formatted correctly after the page is refreshed.
Here's the javascript:
$("#micropost_form").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice))%></div>');
$("#user_info").html("<%= pluralize(current_user.microposts.count, "micropost") %>");
$("#feed_items").prepend(" <%= escape_javascript(render(:partial => #micropost)) %>")
$("#micropost_form")[0].reset();
The body of my application layout:
<body>
<div class="container">
<%= render 'layouts/header' %>
<section class="round">
<div id="flash_notice"><%= render 'shared/flash_messages'%></div>
<%= yield %>
</section>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
Here's my home page (rendered in the yield of the application layout):
<table class="front" summary="For signed-in users">
<tr>
<td class="main">
<h1 class="micropost">What's happening?</h1>
<%= render 'shared/micropost_form' %>
<%= render 'shared/feed' %>
</td>
<td class="sidebar round">
<%= render 'shared/user_info' %>
<%= render 'shared/stats' %>
</td>
</tr>
</table>
Here's my feed partial:
<% unless #feed_items.empty? %>
<table id="feed_items" class="microposts" summary="User microposts">
<%= render :partial => 'shared/feed_item', :collection => #feed_items %>
</table>
<%= will_paginate #feed_items %>
<% end %>
My flash messages partial:
<% flash.each do |key, value| %>
<div class="<%= key %>"><%= value %></div>
<% end %>
Let me know if there's any other code I should post.
So what am I doing wrong? If you want to look at all the code and run it locally to get a better understanding of what's happening, you can find it here: https://github.com/meltzerj/sample_app
When you're using ajax, you need to use flash.now for flash messages. See docs here. Rails is looking for a page refresh/redirect to push and pop from the flash hash stack, using now will set it immediately, then you can access it as above. You can use flash.now in replace of your regular flash usage in the controller. Something like this is very typical:
if #object.save
flash.now[:notice] = "Object successfully created"
else
...
end
So that will fix your flash issue.
As for the weird content/styling, it's a bit hard to comment without actually seeing the markup. The only thing that looks odd to me is the render :partial => #micropost
I'm not sure if #micropost is just a string, but you'd normally do something like render :partial => 'path/to/partial', :object => #micropost
Again it's almost impossible to say without seeing all the other markup