I have nested resources and I'm trying to show the new layout for the nested resource on the show of the parent.
resources :discussions do
resources :comments
end
discussions\show.html.erb
<%= #discussion.title %>
<%= ... render the discussion %>
<%= ... render the existing comments %>
<% render 'comments/new' %> <--- trying something like this
comments/new throws an error because it's missing the partial.
comments/form works to get past that, but throws an error saying my #comment is nil.
comments/_form.html.erb
undefined method discussion for nil:NilClass
<%= bootstrap_form_for([ #comment.discussion, #comment] ) do |f| %>
Do I have to change something in the controller, or am I going about this incorrectly?
Thanks for your help!
try this
discussions\show.html.erb
<%= render 'comments/form', comment: #discussion.comments.build %>
comments/_form.html.erb
<%= bootstrap_form_for([ comment.discussion, comment] ) do |f| %>
Hope this will work.
Related
I have the following routes on my project:
namespace :teacher do
resources job_applications do
resources :job_application_addresses
end
My form has the folling code
<%= simple_form_for [:teacher, #job_application_address] do |form|
<% end %>
And my controller has the following:
def new
#job_application_address = JobApplicationAddress.new
end
def create
#job_application_address = JobApplicationAddress.new(job_application_address_params)
#job_application_address.job_application = #job_application
if #job_application_address.save
flash[:success] = 'Successfully created'
end
end
Finally I'm getting this error:
undefined method `teacher_job_application_addresses_path' for #<#<Class:0x00007fda0c4191d0>:0x00007fda143d1af8>
Did you mean? teacher_job_application_path
teacher_job_applications_path
Extracted source (around line #3):
<%= simple_form_for [:teacher, #job_application_address] do |form| %>
This are my routes for this view:
teacher_job_application_job_application_addresses GET /teacher/job_applications/:job_application_id/job_application_addresses(.:format) teacher/job_application_addresses#index
POST /teacher/job_applications/:job_application_id/job_application_addresses(.:format) teacher/job_application_addresses#create
new_teacher_job_application_job_application_address GET /teacher/job_applications/:job_application_id/job_application_addresses/new(.:format) teacher/job_application_addresses#new
edit_teacher_job_application_job_application_address GET /teacher/job_applications/:job_application_id/job_application_addresses/:id/edit(.:format) teacher/job_application_addresses#edit
teacher_job_application_job_application_address GET /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format) teacher/job_application_addresses#show
PATCH /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format) teacher/job_application_addresses#update
PUT /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format) teacher/job_application_addresses#update
DELETE /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format) teacher/job_application_addresses#destroy
What should I do? I'll apprecciate your help.
Your job_application_addresses is nested resource of job_application. But i dont see it in your form.
You can do it by 2 ways:
Your can add job_application object in the simple_form_for url builder like this
<%= simple_form_for [:teacher, #job_application, #job_application_address] do |form|
<% end %>
You can add an url option in the form
<%= simple_form_for [#job_application, #job_application_address], url: teacher_job_application_job_application_addresses_path do |form|
<% end %>`
In both cases you need to have #job_application object in new action in controller.
Hope it helps
I am sorry in advance as I know this should be an easy one but I am stuck. I have a show view for "Category" in which I am trying to display related has_many "Subcategories". I am calling the partial by using the following:
<%= render partial: 'subcategories/subcategory', locals: {category: #category }%>
I have an html file in the Subcategories view folder properly named and the partial view loads. I know this because the partial has the code
<%= #category.name %></p>
which shows the correct Category name within the partial. However, when I try to load any of the subcategory data by calling
<% #subcategories.each do |subcategory| %>
<%= subcategory.name%>
<% end %>
I get the error: NoMethodError in Categories#show, undefined method `each' for nil:NilClass
I'm sorry to ask such a basic question but I will be using partials from related modules extensively in this project.
Replace this:
<%= render partial: 'subcategories/subcategory', locals: {category: #category }%>
with this:
<%= render #category.subcategories %>
Then in the partial app/views/subcategories/_subcategory.html.erb do this:
<%= subcategory.name %>
I really have no clue why it happen.
this is my routes
resources :users do
resources :bookmarks
end
Controller
# bookmarks_controller
def edit
# this returns perfectly data in the edit view
#bookmark = current_user.bookmarks.find(params[:id])
end
This is the view
<%= form_for #bookmark do |b| %>
<% end %>
Since everything messed up when I nested the bookmarks resources inside users it causes the error
undefined method `bookmark_path'
Then I change the form to
<%= form_for user_bookmark_path(current_user, #bookmark) do |b| %>
The error is gone but there is no such data in the text field form, and the form action is /users/[user_id]/bookmarks/[bookmark_id]/edit
rake routes info
user_bookmarks GET /users/:user_id/bookmarks(.:format) bookmarks#index
POST /users/:user_id/bookmarks(.:format) bookmarks#create
new_user_bookmark GET /users/:user_id/bookmarks/new(.:format) bookmarks#new
edit_user_bookmark GET /users/:user_id/bookmarks/:id/edit(.:format) bookmarks#edit
user_bookmark GET /users/:user_id/bookmarks/:id(.:format) bookmarks#show
PUT /users/:user_id/bookmarks/:id(.:format) bookmarks#update
DELETE /users/:user_id/bookmarks/:id(.:format) bookmarks#destroy
Any idea how to fix it ? Thanks
Try something like:
<%= form_for [current_user, #bookmark] do |b| %>
<% end %>
This is my code for rendering the partial (the #parties collection is being generated correctly, I have tested that):
<% #parties.each do |party| %>
<div class="item">
<%= render 'parties/party', :object => party %>
</div>
<% end %>
And this is the code in the partial:
<%= party.name %>
However, I get the following error:
undefined method `name' for nil:NilClass
I'm at my wits end, someone please help :-|
Also, this is the code for the controller to render the view containing the partial (The controller's called default_controller):
def index
#parties = Party.all
end
Is it of any consequence that this isn't the parties_controller?
I've tried something like below and it worked
<%= render :partial => 'party', :object => party %>
and I can access like party.name. the local variable is named after the partial name which is party here.
Note: Im assuming that your both partials are of parties_controller. So this should work.
Update:
Here is what ive tried with again
class PostsController < ApplicationController
#... ...
def index
#posts = Post.all
#comments = Comment.all #<---- Loading comments from PostsController
#... ...
end
end
#views/posts/index.html.erb
<% #comments.each do |comment| %>
<%= render :partial=>"comments/comment", :object=>comment %>
<% end %>
#views/comments/_comment.html.erb
<%= comment.body %>
And its working :)
I'm new to ruby on rails.
In views/events I have "_form.html.erb" which is rendered in "new.html.erb" by this code:
<%= render "form" %>
Now I want to render "_form.html.erb" in "index.html.erb" which is in the same folder(views/events).
But I get the error "missing template".
I guess I have to add some thing to controller, please help me to render form in other pages of views...
You "usually" don't render a form in an index action. Most form partials are setup semantically to expect a #my_resource, but if you're doing everything the rails way you're not going to have a instance variable during your index action. There's a number of ways you can do this but this is probably the quickest.
You probably have some collection (let's pretend you're using books) in your index action:
#views/books/index.html.erb
<% #books.each do |book| %>
...
<%= render "form" %>
...
<% end %>
You can just set an instance variable somewhere prior to rendering the form:
#views/books/index.html.erb
<% #books.each do |book| %>
<% #book = book %>
...
<%= render "form" %>
...
<% end %>
Another way to do it would be through passing in some locals to a partial. You'd have to change all of your references in _form to use a local variable instead. Then you can call render like this:
<%= render :partial => "form", :locals => {:book => book } %>
You can try
<%= render "events/form" %>
I had this problem before and this solved