Rendering Partial from outside main/index.rhtml...Ruby on Rails - ruby-on-rails

Ok I have posted a couple of things that had to do with my problem but I think I have it narrowed down. Here is where I am at:
I have my index.rhtml page inside of /views/main and the main_controller is set up correctly. I am attempting to make this page a dashboard of sorts so it needs to reference multiple other views to display their index.html.erb page. I will use 'proposals' as our example. I want to display the proposal views/proposals/index.html.erb page in the views/main/index.rhtml side bar. I have gathered that you do this through partials.
So...I created a file, /views/proposals/_index.html.erb that has the same code as views/proposals/index.html.erb.
Then in my views/main/index.rhtml file I have the following code:
<%= render :partial => #proposal %>
Now, I don't get an error message, simply nothing is displayed. I don't have anything referencing this (I don't think) in my routes.rb file and I suspect that is the problem.
Sorry for the redundancy on this question but I didn't even really know what I was asking. Hope this helps.
UPDATED:
When I put the <%= render :partial => "proposals/index" %> mentioned below I now get this error:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #1):
1: <% #proposals.each do |proposal| %>
2: <div id="proposalindex">
3: <%= link_to_unless_current h(proposal.name), proposal %><br/>
4: <p5>Added <%= time_ago_in_words(proposal.created_at) %> ago | </p5
This partial works within the proposals controller not not sure what this means.

<%= render :partial => "proposals/index" %>

Related

Rails render partial with collection—can't access collection

I have a view calling this:
<%= render 'health_safety/access_requests/access_request_user', collection: #access_request_users %>
And in _access_request_user.html.erb I'm trying to use the collection, e.g.
<%= p access_request_user.inspect %>
And I get the following error:
undefined local variable or method `access_request_user' for #<#<Class:0x007fc8f0160790>:0x007fc8ebf70100>
I'm absolutely stumped, tried heaps of things but nothing's working. If it makes a difference, #access_request_users is a bunch of User objects pulled from the database.
Try defining the render as a partial, and if this alone doesn't work try defining the name of the variable:
<%= render partial: 'health_safety/access_requests/access_request_user', collection: #access_request_users, as: :access_request_user %>

rails partial not found but it exists

inb4 read the docs
I've already tried everything in the documentation and everything on stackoverflow just repeats the documentation.
I'm trying to render a partial from within another view inside a gem being used by the application:
<%= render :partial => "#{Rails.root.to_s}/app/views/layouts/login" %>
It's complaining that the partial isn't found even though I know it exists.
Missing partial /home/hstorres/src/<app-name>/app/views/layouts/login
The following command was done from within the app containing the partial:
$ ls /home/hstorres/src/<app-name>/app/views/layouts
application.html.erb _login.html.erb
So if it exists, and it's looking for it in the right place, why can't it find it?
Please try this
<%= render :partial => "layouts/login" %>
You could use
<%= render "layouts/login" %>
View paths in Rails are relative to /app/views/. Simply typing render :partial => "layouts/login" will do what you want.

RoR undefined method 'to_datetime' depending on order of helper methods

I am experiencing a very odd problem; I get this error
undefined method `to_datetime' for nil:NilClass"
when render 'modal' is before render 'post' in show.html.erb, but not if it is after:
<div class="page-header">
<h2>Indlæg af <%= #user.first_name %></h2>
</div>
<% if current_user == #user %>
<%= render 'modal' %>
<% end %>
<%= render 'post' %>
I have attached the views in the gist underneath:
Gist: https://gist.github.com/czepluch/8166841
It makes no sense to me that this error occurs depending on the order the renders are placed in. So I would really like to know why the order of the rendering of the helpers matter?
Please add code to the question so it is saved with the answers here. I suspect it has to do with the fact that your form has this:
simple_form_for([#user, #user.posts.build]
That means a new post is built that has no attributes, which could cause a nil error later on. reverse the order, and this isn't built until after the other code has run. The only thing I can see at first glance that is related to datetime would be:
time_ago_in_words(p.created_at)
If created_at is nil, because of an empty post, that could generate an error.
Play with the form declaration, you may be able to do
simple_form_for([#user, :post])
or something like that to get a form for a new post without actually attaching an empty object to the #user object.

Multiple forms on a single page

I've got an app that's in invite-only beta right now. Problem is, I can't get the invite system to work. :(
On my root page there's a login form (which works just fine), and I'm trying to add a "request invite" form on the same page. I started doing it by putting the form for InviteRequest (ActiveRecord) inside a partial, in the "views" folder for "InviteRequest". The app is definitely calling this partial, but I'm getting the following error:
NoMethodError in User_sessions#new
Showing app/views/invite_request/_new.html.erb where line #2 raised:
undefined method `invite_requests_path' for #<ActionView::Base:0x25b3248>
Extracted source (around line #2):
1: <% #invite_request = InviteRequest.new() %>
2: <% form_for #invite_request do |ir| %>
3: <%= ir.label :email %>
4: <%= ir.text_field :email %>
5: <% end %>
I also read through the "Multiple Models in a Form" section of my trusty copy of "Agile Web Development with Rails", about maybe doing this with a "fieldset" tag, but not sure if this is the right approach.
Thx.
Your form_for #invite_request is trying to figure out where the results from the form should be posted. It does this by looking at the routes and finding one that matches the resource and action needed. In your case I'm guessing you haven't set up routes for InviteRequest. You either need to set up routing for InviteRequest or specify the URL to be posted to in form_for.
form_for #invite_request, :url => { :controller => 'application', :action => 'request_invite' } do |form|
...

Rails: form.error_messages does not wrap custom validated fields in a fieldWithErrors div

I have a model that uses the low level validate_on_create method, based on a simple condition I'm adding an error message to the field, e.g
self.errors.add(:expiry_date, "is too soon") if self.expiry_date && self.expiry_date < Date.today + 1.week
When rendering the view 'new' after a failed save the field isn't picked up by error_messages_for as I would expect. The error is shown in the ususal list of errors that indicated the validation is working correctly.
Has anybody got any idea why this is? I guess form.error_messages isn't looking at all errors on the object?
Added the requested code:
<% form_for([:solicitor, #policy]) do |form| %>
<%= form.error_messages :message => "See the list of reasons below to find out why not:", :header_message => "Sorry but we cannot yet create your policy based on the information you've provided." %>
<%= render :partial => 'form', :locals => {:form => form} %>
<% end %>
More than likely your problem is:
You are not passing the form_for the correct options.
You are redirecting back to the form instead of rendering.
You aren't using the form_for builders (ie text_field_tag instead of f.text_field)
However, without posting your full controller/view, I'm basically just guessing.
The problem was a simple typo in the form partial. I should have posted all the code in question as I'm sure if I did BJ Clark or others would have easily spotted the silly mistake I made
Lesson learned!

Resources