How to render view in rails - ruby-on-rails

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" %>

Related

Errors not displaying when rendering edit in Rails 7

My app is running Rails Rails 7.0.2.3
In my update controller action I have the line:
return render(:edit) unless #user_form.save
This renders the edit view on error .... but errors are not displayed.
In my edit view I am defining the form with:
form_for #user_form, url: user_path(#user_form), method: :patch do |f|
The form submits via turbo. I can see the error being added to #user_form.errors in the controller, but the instance of #user_form in the view is not changing on each form submission. If I output #user_form.inspect to the view - the id remains the same on each submission.
I have tried adding remote: false to the form_for call, but this does not seem to have an effect.
The only solution I have found is to add data: { turbo: false } to the form_for call.
Is there a better way of handling this?
you'll want to use a partial to show the errors.
you'll need to add an update.turbo_stream.erb file to this view's directory. In that file have something like:
<%= turbo_stream.update 'id_of_div_where_you_want_to_show_errors' do %>
<%= render partial: 'partial_that_displays_errors' %>
<% end %>
or your controllers update action you'll want to have something like
respond_to do |format|
format.turbo_stream { turbo_stream.update 'id_of_div_where_you_want_to_show_errors', render partial: 'partial_that_displays_errors' %>
end
Treat all this more like pseudocode and apply it to your own app, it's hard to say what's going on without seeing your code.

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.

correct way to pass errors

I have a form in a view of my project and
Im doing
redirect_to root_path(#locale), alert: #client.errors
and getting the errors in my view iterating with
flash[:alert].full_messages.each
but when there are a lot of errors rails launch the error
ActionDispatch::Cookies::CookieOverflow
what is the correct way to pass a lot of error descriptions in RoR?
Normally, the alert is a set length string - not something that can grow this long.
render :new, alert: "There has been an error"
When you render you can look for the errors in an instance object - in this case #client.errors.
The rails way is something like this in the view...
<% if #client.errors %>
<ul>
<% #client.errors.full_messages.each do |m| %>
<li><%= m %></li>
<% end %>
</ul>
<% end %>
Forgive my erb - I've been writing haml too long.
The main thing to take away from this is to render instead of redirect. Usually the page that's rendered is the same one that had the form. So, in the create method, you render new on error. In the update method, you render edit.
Personally, I think that the most correct way to pass error is to use a redirect_to only if you pass the validation, and a render if you don't (a simple test on the value of #object.update_attributes(hash)).
This way, you can directly access your object errors from inside your view (through #object.errors) without loading anything in the user cookies.

NoMethodError in Rails: Passing variables

I'm trying to pass in the variable "post" to a partial. The partial is being used on both my show#view & I'm also rendering a collection using it. Here's what it looks like (notice the "#"):
##Show#View
<%= render 'my_partial/my_view', post: #post %>
##Collection ## (I'm not using the "#" symbol)
<%= render 'my_partial/my_view', post: post %>
#Mypartial
<% if #post.something? %>
## do this
<% else %>
## do that
<% end %>
And then I get the beautiful NoMethodError undefined methodsomething?' for nil:NilClass` page when using it in my collection. I know why I'm getting it, I'm just wondering what's the DRY way(s) of getting this to work? Should I just create another partial?
Thank you
Gave my solution below.. Though, it's probably not the best way...
Why are you referencing #post in your partial? You should use post instead, that is the entire point of what you are doing (passing variables to a partial as local variables).
You must change the rendering to:
<%= render 'my_partial/my_view', locals: { post: #post } %>
Take a look in the rails guide: http://guides.rubyonrails.org/layouts_and_rendering.html
Search for "3.4.4 Passing Local Variables" and you will find more information about this whole stuff.
And for fixing your collection problem also check out the rails guide and search for: "3.4.5 Rendering Collections".

Rendering Partial from outside main/index.rhtml...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" %>

Resources