Render partial error: undefined method `keys' for Array - ruby-on-rails

I am allowing the dispenser to approve the dispenser_review using a remote link in rails. The approval in the DB is working, but the updating of the link on the page is not.
Rendered dispenser_reviews/update_approved_review.js.erb (1.6ms)
Completed 500 Internal Server Error in 22.4ms
NoMethodError - undefined method `keys' for #<Array:0x0000000e632500>:
actionpack (3.2.17) lib/action_view/renderer/partial_renderer.rb:330:in `find_partial'
dispensers/show.html.erb
<% #dispenser_reviews.all.each do |review| %>
<p id="approval<%= review.id %>"><%= render :partial => 'approve_review', locals: { review: review } %></p>
<% end %>
dispensers/_approve_review
<% if review.approved %>
<i class="icon-check-sign"></i>
<% else %>
<%= link_to "Approve", approve_dispenser_review_path(review), :remote=>true %> <%= link_to "Approve with response", "#" %>
<% end %>
dispenser_reviews_controller.rb
def approve_dispenser_review
#review = DispenserReview.find(params[:id])
#review.approved = true
#review.save
respond_to do |format|
format.js { render :action => "update_approved_review", :layout => false }
end
end
dispenser_reviews/update_approved_review.js.erb
$("#approval<%= #review.id %>").html("<%= escape_javascript(render(:partial => 'dispensers/approve_review', locals: [review: #review] )) %>")

Update dispenser_reviews/update_approved_review.js.erb as
$("#approval<%= #review.id %>").html("<%= escape_javascript(render(:partial => 'dispensers/approve_review', locals: {review: #review} ))
locals: {review: #review} and not locals:[review: #review].
Pass a Hash not an Array of Hash.

Related

Why Ajax call is responding after a page refresh in rails 4?

Suppose I have defined my create action like this:
def create
#appointment = Appointment.find(params[:appointment_id])
#note = #appointment.notes.new(notes_params)
#note.user_id = current_user.id
respond_to do |format|
if #note.save
format.html { redirect_to appointment_path(#appointment), notice: "Saved successfully" }
format.js
else
format.html { render 'new', notice: "Try again" }
format.js
end
end
end
My create.js.erb is like this:
$("#note_field").html("<%= j render partial: 'note', locals: {note: #note} %>");
Here is my form:
<%= form_for([#appointment, #appointment.notes.build({user: current_user})], remote: true) do |f| %>
<div>
<% if #note && #note.errors.any? %>
<% #note.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<% end %>
</div>
<div class="field">
<%= f.label :Description %><br>
<%= f.text_area :description, required: true %>
</div>
<div>
<%= f.submit class: "note-button" %>
</div>
<% end %>
Here, note is partial form. How to make it work without a page refresh?

Rails don't render layout when create record with Ajax

My problem is after I create a new record with ajax I get a NOMETHODERROR and I must reload the page to see the new record.
The Error I get in terminal.
Rendered time_entries/_time_entry.html.erb (3.4ms) Rendered
time_entries/create.js.erb (4.9ms) Completed 500 Internal Server Error
in 41ms (ActiveRecord: 4.4ms)
NoMethodError - undefined method `each' for nil:NilClass:
app/views/time_entries/_time_entry.html.erb:1:in '_app_views_time_entries__time_entry_html_erb___4086905375499854267_70174828432680'
What I do wrong?
time_entries_controller.erb
class TimeEntriesController < ApplicationController
...
respond_to :html, :js
def index
#time_entries = current_user.time_entries.all.order("date DESC")
#time_entries_days = #time_entries.group_by{ |t| t.date.beginning_of_day }
respond_with(#time_entry)
end
def create
#time_entry = TimeEntry.new(timeentry_params)
#time_entry.user_id = current_user.id
respond_to do |format|
if #time_entry.save
format.html { redirect_to #time_entry, notice: 'Arbeitszeit wurde eingetragen' }
format.json { render :show, status: :created, location: #time_entry }
format.js
else
format.html { render :new }
format.json { render json: #time_entry.errors, status: :unprocessable_entity }
format.js
end
end
end
...
_time_entry.html.erb
the partial
<% #time_entries_days.each do |day, time_entries| %>
<% if day.today? %>
<div class="column-12 list-group">Today, <%= l day, format: :dm %></div>
<% else %>
<div class="column-12 list-group"><%= l day, format: :dm %></div>
<% end %>
<ul class="lists">
<% time_entries.each do |time_entry| %>
<li class="list-item" id="time_entry_<%= time_entry.id %>">
<div class="list-item__content">
<h3 class="list-item__title">
<%= link_to time_entry.category.name, time_entry %>
<span><%= l time_entry.start_time, format: :hm %></span> - <span><%= l time_entry.end_time, format: :hm %></span>
</h3>
<p><%= time_entry.note %></p>
</div>
<span class="list-item__label"><%= time_entry.hours %>h</span>
</li>
<% end %>
</ul>
<% end %>
index.html.erb
<% if can? :create, TimeEntry %>
<%= link_to 'Add new Time Entry', new_time_entry_path, remote: true, class: 'btn btn-primary', :data => { :'popup-open' => 'popup-1' } %>
<% end %>
...
<div class="row" id="container_time_entries">
<%= render "time_entry" %>
</div>
create.js.erb for testing
I don't get an alert after I create a new time entry.
alert("HALLO");
create.js.erb normal
$('.modal-bg').hide();
$('.popup').fadeOut(350);
$('#container_time_entries').html("<%= j(render 'time_entry') %>");
You do not set the variable #time_entries_days in your create method, but you need it in your _time_entry.html.erb .
Set it and you should be good to go.

Passing a block variable in an js.erb ajax call

I'm trying to make an ajax call but get a 500 (Internal Server Error) due to a template error. Here's what I get in my server logs:
ActionView::Template::Error (undefined local variable or method `dish' for #<#<Class:0x007fcd62f1b008>:0x007fcd6516bab8>):
1: <% if current_user.liked? dish %>
2: <%= link_to "Unlike", unlike_restaurant_dish_path(#restaurant, dish), method: :put, remote: true %>
3: <% else %>
4: <%= link_to "Like", like_restaurant_dish_path(#restaurant, dish), method: :put, remote: true %>
app/views/restaurants/dish_partials/_like_toggle.html.erb:1:in `_app_views_restaurants_dish_partials__like_toggle_html_erb___2598971629205628451_70260057733340'
app/views/dishes/like.js.erb:1:in `_app_views_dishes_like_js_erb__1312876563634492607_70260057701620'
app/controllers/dishes_controller.rb:6:in `like'
From what I understand its not finding the block variable dish that I'm passing in my each block in my partial. What needs to be done so that they're recognized?
Dishes Controller
class DishesController < ApplicationController
before_action :load_restaurant_and_dish, only: [:like, :unlike, :dislike, :undislike]
def like
#dish.liked_by current_user
respond_to do |format|
format.html { redirect_to #restaurant }
format.js
end
end
...
private
def load_restaurant_and_dish
#restaurant = Restaurant.find(params[:id])
#dish = Dish.find(params[:id])
end
end
views/dishes/like.js.erb
$(".restaurant__dish").html('<%= escape_javascript(render partial: "restaurants/dish_partials/like_toggle", dish: dish) %>');
views/restaurants/dish_partials/_dishes.html.erb
<% #dishes.each do |dish| %>
<div class="restaurant__dish">
<b><%= dish.name %></b>
<%= render "restaurants/dish_partials/like_toggle", dish: dish %>
</div>
<% end %>
views/restaurants/dish_partials/_like_toggle.html.erb
<% if current_user.liked? dish %>
<%= link_to "Unlike", unlike_restaurant_dish_path(#restaurant, dish), method: :put, remote: true %>
<% else %>
<%= link_to "Like", like_restaurant_dish_path(#restaurant, dish), method: :put, remote: true %>
<% end %>
<% if current_user.disliked? dish %>
<%= link_to "Undislike", undislike_restaurant_dish_path(#restaurant, dish), method: :put, remote: true %>
<% else %>
<%= link_to "Dislike", dislike_restaurant_dish_path(#restaurant, dish), method: :put, remote: true %>
<% end %>
views/dishes/like.js.erb
$(".restaurant__dish").html('<%= escape_javascript(render "restaurants/dish_partials/like_toggle", dish: #dish) %>');
or
$(".restaurant__dish").html('<%= escape_javascript(render partial: "restaurants/dish_partials/like_toggle", locals: { dish: #dish }) %>');
you're missing # before dish variable.

Value update automatically though javascript is off

In my application I am showing all the products like this
store.html.erb
<% #products.each do |product| %>
<div class="entry" >
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line" >
<span class="price" ><%= number_to_currency(product.price) %></span>
<%= button_to "add to cart", line_items_path(:product_id => product),
:remote => true %>
</div>
</div>
<% end %>
I am sending ajax request with :remote => true and in my line items controller I don't have any .js format
line_items_controller.rb
def create
#cart = current_cart
product = Product.find(params[:product_id])
#line_item = #cart.add_product(product.id)
respond_to do |format|
if #line_item.save
format.html { redirect_to(store_url) }
format.xml { render :xml => #line_item,
:status => :created, :location => #line_item }
else
format.html { render :action => "new" }
format.xml { render :xml => #line_item.errors,
:status => :unprocessable_entity }
end
end
end
What I understand so far is that, since there is no format.js add to cart should not add any product to the cart and nothing should happen. But when I add .js format in the controller
format.js { #current_item = #line_item }
and update the page I see that add to cart worked behind but did not show the result. after updating the page I see 15 or so items in the cart. How is this happening?
Make changes here
<% #products.each do |product| %>
<div class="entry" >
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line" >
<span class="price" ><%= number_to_currency(product.price) %></span>
<%= button_to "add to cart", line_items_path(:product_id => product.id),
:remote => true %> #changes line
</div>
</div>
<% end %>
and for update you have to make .js file for that and update elements and onject of html page through jquery or javascript.
And use only one format so controller method go to one responsive area.

How to use partials with more than one collection in rails

I want to use two collection #ad_item and #user in my partial. Here is my index erb...
<% if #ad_items.any? && #user.any? %>
<%= render partial: 'yourads/ad_item', collection: {#ad_items,#user} %>
<% end %>
and here is my controller ...
def index
#ad_items = Yourad.all
#user = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #yourads }
end
end
and here is my _ad_item.erb.html partial
<span class="date"><%= ad_item.created_at.to_date() %></span>
<span class="location"><%= ad_item.title %></span>
<div style="float:left"><%= ad_pic #user,ad_item %></div>
<div class="description"><%= ad_item.description %></div>
My helper function is ..
def ad_pic(user,ad)
cl_image_tag("Ad#{user.id}#{ad.id}.jpg", :version => rand(1000000000), :alt => "Ad pic",:width => 70, :height => 70, :crop => :fill)
end
it gives syntax error in ..
<%= render partial: 'yourads/ad_item', collection: {#ad_items,#user} %>
<%= render partial: 'yourads/ad_item', collection: #ad_items ,
locals: {users: #users} %>
and in view
<% for user in users %>
<span class="date"><%= ad_item.created_at.to_date() %></span>
<span class="location"><%= ad_item.title %></span>
<div style="float:left"><%= ad_pic user,ad_item %></div>
<div class="description"><%= ad_item.description %></div>
<% end %>
see Passing Local Variables
<%= render partial: 'yourads/ad_item', :locals => {:ad_items=>#ad_items,:user => #user }%>
you can access #ad_items as ad_items and #user as user in your yourads/ad_item partial

Resources