Is there a way to display the new.html.erb form_for div in index.html.erb?
I'd like to be able to add a new entry without leaving the homepage. Maybe a partial?
You can't render part of the template, only whole templete.
If you have a part that same for several templates, than put it to the partial and render partial as part of the template.
This is very simple example:
_partial.rb:
<%= form_for ... %>
...
<% end %>
new.html.erb:
render 'partial'
index.html.erb:
render 'partial'
More about partials and rendering you can read in rails guides.
I got it to work by creating a #article = Article.create in the index module of the controller. Works like a charm. No partials needed.
This method works for me.
In the links_controller.rb I added the #link to the index controller
def index
#links = Link.all
#link = Link.new
end
In the index.html.erb I added only the partial
<%= render 'form' %>
Ended up with the screen of this:
Yes partial would be better in that case. You can have a partial for that "_form.html.erb".
In that partial just put the form_for content. Let you have a Blog model and in the index you are showing the blog lists.
def index
#blog = Blog.new
#your rest code goes here
end
index.html.erb
<YOUR HTML CONTENT>
<%= render "/blogs/form" %>
_form.html.erb
<%= form_for(#blog) do |f| %>
Your code for the form
<%= end %>
If you want to keep the new action will show only the form, then you need to change the action and new.html.erb
def new
#blog = Blog.new
end
In new.html.erb
<%= render '/blogs/form' %>
Related
I'm trying to render a the index view inside my ruby-on-rails application. How do a I render the index view, from inside a view passing an array of things to display? Using the link_to.
I do not want to re-route to the controller, I just want to link_to a view passing it the variables it needs, how can I do this?
EDIT:
I am trying to create a page type functionality in the index of my article model. So I have around 400 articles for example, and when the index action in the article controller is called, it of course renders the index view which is calling a partial for each article in the '#articles array' passed on by article controller's index action.
So in the view, I'm trying to do something like:
<% count = 0 %>
<% #articles.each do |article| %>
<% if count <10 %>
<%= render partial: 'index_articles', locals: {article: article} %>
<% count = count + 1 %>
<% end %>
<% end %>
<% #articles = #articles.drop(10) %>
<% if #articles.any? %>
<%= link_to "Next", 'articles', locals: {#articles => #articles} %>
<% end %>
Thank you in advanced for all of your help.
You'll need to use the render command, probably with a partial:
<%= render "controller/index", collection: ["your", "array"], as: :object_name %>
You will have to call a controller action to generate this. You cannot simply load it on your screen, unless it was preloaded inside your javascript for something:
#View
<%= link_to "Index", controllers_path(ids: ["1","2"]), remote: true %>
#app/controllers/your_controller.rb
class YourController < ApplicationController
def index
#posts = request.xhr? Post.find(params[:ids]) : Post.all
respond_to do |format|
format.js #-> app/views/controller/index.js.erb
format.html
end
end
end
#app/views/controller/index.js.erb
$(".element").html("<%=j render 'index' %>");
There are several issues with this approach...
Flow
First of all, your flow of your app should be as structured as possible.
In short, if you're calling the index view inside another action, it's not the index view any more.
What you should look at is how to use a partial in your app:
#app/controller/views/_partial.html.erb
<%= post.title %>
This way, you can adapt your index view and your other page to use the partial in their respective action layouts:
#app/controller/views/index.html.erb
<%= render "partial", collection: #posts, as: :post %>
This will allow you to "reuse" code much in the way you want. This will be much more appropriate than trying to invoke other action/views.
-
Resources
Secondly, you'll want to look at how your app functions.
Your index view is meant to show all the items for a particular object. Whilst you're free to change this as you want, the fact remains that you have to keep some structure.
You should read up on the routes for your actions, and how they're meant to work in your application. This will give you some perspective on the resourceful nature of Rails routes, and how you'll have to call specific routes with specific actions.
Your problem is probably that the file needs to be named _index.html.erb. You can have another file named index.html.erb which just renders _index.html.erb.
If you need a full guide on using AJAX, look up the railscast. If you're not using AJAX and you just want to render it, then you don't use link_to. You just do <%= render :index %>.
I'm trying to render a view in a different controllers view but I'm getting:
undefined method `each' for nil:NilClass
I'm rendering the view in 'views/users/show' as:
<%= render :template => 'groups/index' %>
The view itself is under 'views/groups/index':
<% #groups.each do |group| %>
<li>
<%= group.name %>
<%= group.description %>
</li>
<% end %>
And my groups controller for index looks like this:
def index
#groups = Group.all
end
I think it's a problem with how i'm rendering the view since if I make an instance variable in my index controller and call it in the view it won't appear. There are entries in the Group table in my database.
Any help would be appreciated.
Thanks in advance.
I think it should be enough to replace the template: with a partial: parameter.
Try this:
<%= render partial: 'groups/index' %>
You will have to rename/copy groups/index.html.erb with groups/_index.html.erb
This only works for rendering a view, but will not implement the functionality of your GroupsController.
Edit
You will have to redefine the groups inside your UsersController
# UsersController
def index
#groups = Group.all
end
Depending on how many times you will need to present all these groups to your user, this can become hard to maintain. If you use it frequently, consider adding
# i.e. ApplicationController
def groups
Group.all
end
inside your ApplicationController (or some module you want to include in different controllers). Then you could call
# UsersController
def index
#groups = groups
end
and still <%= render partial: 'groups/index' %>
Change:
<%= render :template => 'groups/index' %>
To:
<%= render 'groups/index' %>
and make sure the file name of your index action is _index.html.erb and not index.html.erb.
EDIT
When you render a view, you are only rendering the template, this does invoke a request on your index action. You must define #groups in your initial view's action.
So I have this _index partial for comments that I want to display under pictures show page.
I am rendering the partial in my media view currently like this:
<div id="comments">
<%= render partial: 'comments/index' %>
</div>
My comments controller looks like this:
def index
#comments = Comment.all
render partial: 'comments/index', locals: { :comments => #comments }
end
And the coffescript code that I use to reload it looks like this:
setInterval (=> $('#comments').load('/comments')), 1000
Now in my view when I try to access the :comments variable that I sent to the partial, I am unable to do it. It contains no values.
How can I get the values properly to that partial?
Worth noting that the instance variable #comments is also not accessible from my partial.
You're rendering the comments index partial from the picture show view.
The action in use here is picture show. When rendering the comments partial it will not call the comments controller. That's why the #comments variable isn't accessible.
So instead define the #comments in your picture controller
def show
#picture = Picture.find(params[:id])
#comments = #picture.comments
end
Then pass them from the picture/show view to your partial
<div id="comments">
<%= render partial: 'comments/index', :locals => :comments => #comments %>
</div>
I want to use same html form for Edit and New methods.
Controller methods:
def new
render "edit"
end
def edit
if params[:id].present?
#goat = Goat.find(params[:id])
else
#goat = Goat.new
end
end
Edit form:
<% form_for #goat do |f| %>
<%= f.text_field :title %>
<%= f.submit "Update" %>
<% end %>
It works well for Edit method when #goat record exists, but for New method, when controller gives #goat = Goat.new I got an error:
undefined method model_name for NilClass:Class
How to fix this?
The problem is that you're not actually setting the new #goat for your html.
In this code:
def new
render "edit"
end
This doesn't call the edit method on your controller; instead it goes straight to the view layer to render the edit.html.erb file. So there's no value for #goat set, and thus the error you get.
Better is code like what #user2191327 provided. You should also look at what the scaffold would generate for your controller; it's a good guide to what idiomatic Rails looks like.
if you only want to use the same html just use render :template
def new
#goat = Goat.new
render template: 'edit'
end
def edit
#goat = Goat.find(params[:id])
end
or better way: use render partial in templates edit.html && new.html
P.S. using action 'edit' instead 'new' not a good idea, so if you look in your routes.txt file, you will see that path to action includes :id of class instance.
I'm trying to render an action in my application.html.erb layout file to display it as a modal box using some jquery scripts. I've heard that i can use render :template => 'spots/new' but it looks like this method is not rendering an action but just a view file.
spots#new
def new
#spot = Spot.new
end
new.html.erb
<%= form_for(#spot) do |f| %>
...
<% end %>
The problem is that when i'm trying to render spots#new with render :template => 'spots/new', i'm getting undefined method 'model_name' for NilClass:Class error. Have you any idea what am i doing wrong ? Thanks in advance
You are correct, render :template => 'spots/new' just renders your view template, it does not call spots#new. You should create #spot instance variable before rendering the template.
In your case probably following code will work:
<% #spot ||= Spot.new %>
<%= form_for(#spot) do |f| %>
...
<% end %>