I want to show comments tree. I moved comment div in another view, and wrote next line in _comments.html.haml :
= render :partial => 'single_comment', :collection => #post.comments.where(:parent_id => nil)
_single_comments.html.haml:
- if comment.id != nil
.comment
.meta
= comment.name
says
.body
= comment.text
.answers
= render :partial => 'posts/single_comment', :collection => #post.comments.where(:parent_id => comment.id)
But browser show me an error:
undefined local variable or method `comment' for #<#<Class:0x00000004e39280>:0x00000004e2f398>
Extracted source (around line #1):
1: - if comment.id != nil
2: .comment
3: .meta
4: = comment.name
I tried to add :as => comment in first line, but it doesn't work. So as a using #comment in partial.
Maybe it's fundamentally wrong?
You have to add :as => :comment on both render lines, remember the answers that are being rendered are rendering this same partial again, so they will try rendering answers too.
Try adding the :as => :comment on both the comments and the answers rendering part.
Related
I cloned and tweak this rails app and I am experiencing some trouble with rendering a partial when on ajax request, in the logs I see that the guilty line is in the registrations_controller.rb (Devise)
class RegistrationsController < Devise::RegistrationsController
def create
build_resource
if resource.save
if resource.active_for_authentication?
sign_in(resource_name, resource)
(render(:partial => 'thankyou', :layout => false) && return) if request.xhr?
respond_with resource, :location => after_sign_up_path_for(resource)
else
resource.update_attribute(:encrypted_password, nil) # make sure there is no password
expire_session_data_after_sign_in!
(render(:partial => 'thankyou', :layout => false) && return) if request.xhr?
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
render :partial => 'email_capture', :action => :new, :layout => !request.xhr?**
end
end
protected
def after_inactive_sign_up_path_for(resource)
'/thankyou.html'
end
def after_sign_up_path_for(resource)
redirect_to root_path
end
end
The error message returned is this:
ActionView::MissingTemplate - Missing partial
with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder,
:coffee, :haml]}. Searched in: *
"/Users/Davide/Documents/Jobsite/rails-prelaunch-signup-1click/app/views"
* "/Users/Davide/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/devise_invitable-1.1.8/app/views"
* "/Users/Davide/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/devise-2.2.4/app/views"
Interestingly if I remove the :layout => !=request.xhr? parameter the partial gets found but the page refreshes and the loses all the stylesheets and other assets.
Any idea where should I start looking?
Thanks!
I have also cloned and modified that project, and ran into nearly the same issue. For me, the problem would appear after the second failure of saving a bad email address--but the result was identical.
By using context and information from the following question/answer:
form returned from AJAX request is not recognized as 'remote' when submitted
I was able to make a change that resolved my issue. In _email_capture.html.erb, add :remote => true, to the simple_form_for line. In my case, the whole revised line is:
<%= simple_form_for resource, :as => resource_name, :remote => true, :url => registration_path(resource_name) , :html => {:class => 'form-inline'} do |f| %>
Hope this helps you, too!
I have this in my controller
...
..
render :js => "window.location = '/bar/ready'", :locals => { :foo => "foo" }
..
...
I want the <% foo %> variable and I want to access it in my HTML page.
but I cant seem to access it, as <%= params.inspect %> returns
{"controller"=>"bar", "action"=>"ready"}
So how can I pass the variable in my HTML page?
I needed something similar. Late, but I finally manage to do it doing:
render :js => "window.location = #{your_path(:foo => 'foo').to_json}"
And then parse json data in your view
I am experiencing a similar problem as this previous SO question, but the answer doesn't seem to fit me. So I wanted to follow up with my issue.
I have an action in my controller that updates my Profile model using a checkbox. When I click the checkbox with the app running locally I have no problems. However when I deploy to Heroku I get:
ActionView::MissingTemplate (Missing template profiles/show_hometown_settings with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/app/app/views", "/app/vendor/plugins/rails_log_stdout/app/views", "/app/vendor/plugins/rails3_serve_static_assets/app/views", "/app/vendor/plugins/paperclip/app/views"):
I added both a _show_hometown_settings.html.erb file and a _show_hometown_settings.js.erb file but neither did the trick. Checking my checkbox gave me vanilla code on my screen and the above error.
If I add show_hometown_settings.html.erb I no longer get vanilla code. I see the show_hometown_settings.html.erb template, but I still get the error and I don't get redirected to my settings_path, which is the whole point (check to update, post to db, redirected to make further settings changes). Can anyone help me out with this?
Here is the controller action:
def show_hometown_settings
#profile = current_user.profile
if #profile.show_hometown == true
if #profile.update_attributes(:show_hometown => false)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
elsif #profile.show_hometown == false
if #profile.update_attributes(:show_hometown => true)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
end
end
Here's the form I use for the action:
<%= form_tag({:action => "show_hometown_settings", :controller => "profiles"}, :html => {:multipart => true }) do %>
<%= check_box_tag(:show_hometown, 1, #user.profile.show_hometown ? true : false) %>
<%= #user.profile.hometown %>
<% end %>
UPDATE: Here is the part of my routes.rb file that references the action:
match "profiles/show_hometown_settings", :to => "profiles#show_hometown_settings"
UPDATE 2: Following the question below, I got a different error and my logs show a route issue:
This returns
`The page you were looking for doesn't exist (404)`
And in my heroku logs --tail I see
2012-01-19T23:13:47+00:00 app[web.1]: Started POST "/profiles/show_hometown_settings" for 98.218.231.113 at 2012-01-19 23:13:47 +0000
2012-01-19T23:13:47+00:00 app[web.1]:
2012-01-19T23:13:47+00:00 app[web.1]: ActionController::RoutingError (No route matches "/profiles/show_hometown_settings"):
If I change the route from :via => [:put] to :via => [:post] it works. Hopefully that's alright.
You shouldn't need to have that template file at all since you aren't rendering a view, you're only redirecting.
What's probably happening is that your if-elsif logic is breaking down and the code is never reaching those redirects (because neither of the if blocks are evaluating to true). If Rails doesn't get a redirect_to call, then by default it will render the view for the controller action (which is what it is trying to do).
I would change it around to this:
Route:
match "profiles/show_hometown_settings", :to => "profiles#show_hometown_settings", :as => :show_hometown_settings, :via => [:put]
Controller:
def show_hometown_settings
if current_user.profile.update_attributes(:show_hometown => params[:show_hometown])
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
end
Form:
<%= form_tag(show_hometown_settings_path, :method => :put) do %>
<%= check_box_tag(:show_hometown, 1, #user.profile.show_hometown ? true : false) %>
<%= #user.profile.hometown %>
<% end %>
OK, so Ive set up my mailer in Rails which works fine, but I wanted to make a new action (or maybe just a view?) to have a slimmed down contact form in a lightbox. I can do that all fine and dandy but it would use the default layout which I dont want. So I added:
render :layout => 'lightbox'
to the action so that I could use a new layout. Unfortunately that seems to block off my access to the model as I get this error when the lightbox pops up
undefined method `model_name' for NilClass:Class
#on this line
<% form_for #contact, :url => {:action => "create"}, :html => {:method => :post} do |f| %>
So by using a different layout I cant use the resources I set up in my routes which is here:
resources :contacts, :only => [:new, :create], :as => :contacts
#Im passing in a name to the email form
match "contacts/direct/:name" => "contacts#direct", :as => :direct_email
I hope that made sense. But what do I do?
I have list of images and for every image I have link for deleting. That link looks this:
<%= link_to 'delete image', {:controller => 'shops', :action => 'delimg', :imgid => u.id}, :confirm => 'Really?' %>
def logo
puts params[:imgid]
...
end
And I am getting an error Couldn't find Shop with ID=logo and app/controllers/shops_controller.rb:17:in `show' - I tried to add puts 'IN SHOW and it really looks, that after click on that link is called method show. I have no idea, how it is possible...
Could someone help me, please, where is the problem?
This is probably how I would have done it:
#routes.rb
resources :shops do
delete :delimg, :on => :member
end
By adding that, there will be a defined route to the delimg action mapped to the delete method. And that makes it possible to do the following in the view:
<%= link_to 'delete image', delimg_shop_path(u.id), :method => :delete %>
delimg_shop_path is a path helper that exists because of what was added in the routes.rb
you are displaying params[:ingid] in the logo method but in the link to action u have specified delimg??
modiefy your link to as
<%= link_to 'delete image', {:controller => 'shops', :action => 'logo', :imgid => u.id}, :confirm => 'Really?' %>
then it will work