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.
Related
i am using rails for my application.i have generated scaffold for crud operation.
i have scaffold for product_lists. i have page called user_dashboard.html.erb here i want to render pages from product_lists.
i have user_dashboard.html.erb page in pages/ directory..i want to render index.html page from product_lists directory inside user_dashboard.html.erb. i have tried using render.. but its showing error
here is what i have tried
<div class="panel-body">
<%= render 'product_lists' %>
</div>
error:
ActionView::MissingTemplate in Pages#user_dashboard
How can i solve this error and why is this raised?
It is not a partial (starts with underscore), so you need render template. You also need the full path of the template, from the view directory:
<%= render template: 'product_lists/index' %>
if its not within the same folder you need to explicit tell the path.
the error message is showing where rails is looking for.
<%= render "product_lists/index" %>
I am using Ruby on Rails 3.1 and I would like to know how to correctly handle internationalization related to partial template files. That is, ...
... in my app/views/users/flag.html.erb file I have:
<%= t('.test_key1') %>
<%= render :partial => "/users/flag_form" %>
... in my app/views/users/_flag_form.html.erb file I have:
<%= t('.test_key2') %>
If in my config/locales/views/users/en.yml file (note: I am organizing files as stated in the official RoR guide) I use
en:
users:
flag:
test_key1: Test 1 text
test_key2: Test 2 text
the Test 1 text is displayed in the "main" template (app/views/users/flag.html.erb) but the Test 2 text isn't for the partial template (app/views/users/_flag_form.html.erb). How could\should I solve this issue so to properly display the Test 2 text?
config/locales/en.yml
en:
users:
flag:
test_key1: Test 1 text
flag_form:
test_key2: Test 2 text
app/views/users/flag.html.erb
<%= t('.test_key1') %>
<%= render :partial => "/users/flag_form" %>
app/views/users/_flag_form.html.erb
<%= t('.test_key2') %>
NB:
Rails path to the view must match YAML path to the symbol. You need to create an entry at YAML file that matches the name of the view. Omit the trailing underscore since it's a partial.
Read more about lazy lookups
One way would be to using scope, instead of "lazy loading" using the full stop.
Something like this should work:
I18n.t :test_key2, :scope => 'users.flag'
or use:
I18n.t "users.flag.test_key2"
Lastly, you could even pass it to the partial as in
<%= render :partial => "/users/flag_form", :locals => { :test_key => t('.test_key1') } %>
You should also checkout the appendix section on this website as it might be listing something that I am missing:
https://web.archive.org/web/20120619002316/http://www.unixgods.org/~tilo/Rails/where_is_Rails_trying_to_lookup_L10N_strings.html
I wrote this. What do you think about it?
def translate_for_partials key, *args
I18n.t("#{params[:controller].gsub('/', '.')}.#{params[:action]}.#{key}", *args)
end
Is that bad to make such a method ?
I want to know if the code below is correct. I simply want to render a partial found inside a subfolder.
My view folder is like this : Views/Subs/showPartials/_showform1.html.erb
The code below is not working and is displaying the error message <<ActionView::MissingTemplate in Subs#show >>
CODE:
<%= render :partial => 'showPartials/showform1' %>
Many many thanks in advance for your help
Provide the full path to partial:-
<%= render :partial => 'Subs/showPartials/showform1' %>
Thanks.....
Provide full path is the key
Like:
= render 'clients/index/home'
= render 'clients/index/clients'
= render 'clients/index/footer'
i am facing a precarious condition here. I need to partially render a page that does not have a leading underscore.
<%= render(:partial => "contact" ,:controller=>"home") %>
this will look for
app/views/home/_contact.html.erb
but i want it to look for
app/views/home/contact.html.erb
is there a way of doing this.?
Thanks
<%= render :file => '/homes/contact' %>
You should not try to bypass the conventions if not really necessary. I guess contact.html.erb contains a form. Put this into app/views/home/_contact.html.erb and render it in app/views/home/contact.html.erb.
Or as fl00r answered:
<%= render :file => '/homes/contact' %>
As eteubert points out, one of the strengths of Rails is its opinionated nature. What you are trying to do here is bend that to your will. Don't. If you need to render something in another page as a partial, then you really should follow convention and extract a partial from the original page. If there's a form in that page for example, extract it out into a partial and have the original page render the partial as well.
You'll find the less you try to fight Rails, the easier things become.
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" %>