Ruby on Rails partial not rendering properly - ruby-on-rails

I have an Opportunity model and a User model. On my Opportunity index page, I have a table that lists all of the Users. When I click "Show" next to Users, the page that renders is a blank page, whereas it should say "test test test". Here is my code:
views/opportunities/index.html.erb:
<td><%= link_to 'Show', user %></td>
views/users/show.html.erb:
<% render #user %>
views/users/_user.html.erb:
test test test test
Please help!! Thanks!!

<% render #user %> will not display any result, use <%= render #user %> to display the result of the evaluated code.

Executes the ruby code within the brackets.
<% %>
Prints something into erb file.
<%= %>
Avoids line break after expression.
<% -%>
Comments out code within brackets; not sent to client (as opposed to HTML comments).
<%# %>

Related

Rails 6: Render a partial if only I'm using a method from my controller

I have a Users controller, with index, show, edit and destroy methods. In my layouts/ folder, I have a general-purpose user.html.erb layout that renders some partials. These partials are of course producing errors due some of the info isn't available, like #user.name, for example. I've tried to render that partial always when I'm in a def show state, something like:
<% if Users.show %>
<% render "shared/asides/users" %>
<% else %>
Other partials
<% end %>
I've tried several ways and I always get errors. I feel totally lost even trying to find out this on the Rails documentation nothing seems to be indicated there too.
Your problem is, as you say, you're trying to display things associated with a user, like #user.name, but there is no #user.
So why not check for #user before showing the partial? Or if you have a collection of users, I'm guessing #users?
<% if #users %>
<%= render "shared/asides/users" %>
<% else %>
<%= Do something else %>
<% end %>
Of maybe a bit neater:
<%= render (#users ? path/to/partial_a.html.erb : path/to/partial_b.html.erb) %>
You can make a special layout for your action. Then, at the end of action add layout to render.
def show
...
render layout: "custom_layout"
end

I searched passing params in a tags in your views but I did not find anything that can help me

I am building a simple Rails app that has Users, Blogs, and Comments. The last thing I have to do is make a link on the blogs show page where comments show and have those links send the user to the comment/:id/edit page where they can edit their comment.
However, I'm not able to grab the comment ID correctly for some reason and even though I am getting thrown no errors I am not brought to the error form, just a blank web page that has my layout elements.
Here is my blogs controller's show action:
def show
#blog = Blog.find_by_id(params[:id])
#comment = Comment.new
# #current_comment = Comment.find(params[:id])
end
Here is my comments controller's edit action:
def edit
#comment = Comment.find_by_id(params[:id])
end
And here is where the link is in the Blogs show view:
<% #blog.comments.each do |c| %>
<div><%= c.content %></div>
<%= link_to 'Delete', "/comments/#{c.id}", method: :delete %>
<% end %>
The delete function works, but when I try to grab the comment id in the same way for the link to the comment view it does not work. Instead of seeing a number in the url like 1, 2, or whatever the comment id is, I see the #{c.id} syntax. Any thoughts?
<% #blog.comments.each do |c| %>
<div>
<%= link_to c.content, edit_comment_path(c) %>
</div>
<%= link_to 'Delete', c, method: :delete %>
<% end %>
In general you want to avoid hardcoding paths when possible as it makes your code more brittle and verbose.
As you have written it "/comments/#{c.id}/edit" is not even being evaluated since its not inside a ERB tag.
I suppose you mean this part:
<div><%= c.content %></div>
That's because the <div><a href="/comments/#{c.id}/edit"> part is outside a ERB block, therefore it is being interpreted as plain HTML, and that's what you see in the url. There's no variable expansion in plain HTML inside a ERB file.
Either use link_to:
<div><%= link_to c.content, "/comments/#{c.id}/edit" %></div>
or enclose the url part in ERB tags:
<div><%= c.content %></div>

how to print user_id using the devise gem

I currently trying to do something like this in the applicaiton.html.erb
<h1> <% print "Customer id #{current_user}"%> </h1>
but I get nothing. the 'Customer id' is not being printed either. What am I doing wrong ?
Thanks for your time
If you're in a Rails view, using erb, then print or puts are not needed.
The <%= %> allows you to render (print) the expression that's contained inside, while <% %> allows you to create Ruby logic without rendering it, the same way <%# %> works for comments, so you could do:
<h1>
<%= "Customer id #{current_user.id} "%>
</h1>
Or:
<h1>Customer id <%= current_user.id %></h1>
No need to concatenate, and in both cases accessing to the current_user id attribute, otherwise it'd return the whole User object.

Flash Message (Rails v.4.1.1) Curly Brackets

I'm creating a blog on rails. Everytime a post is created on the new page I want to be back at the index page and see a flash message saying "You post was saved". On the layout I have the following:
<body>
<%= flash.each do |key, value| %>
<p><%= value %></p>
<% end %>
<%= yield %>
</body>
The problem is that I have now on every page (index, new, etc.) curly brackets on the top and I don't know why. Also, instead of just having a message "Your post was saved.". It also appears the following: "{"notice"=>"Your post was saved."}". This is the code for the message hold on the controller (posts_controller.rb):
def create
#post = Post.new(post_params)
if #post.save
redirect_to posts_path
flash[:notice] = "Your post was saved."
else
render "new"
end
end
I'm beginning with Rails, thanks for the help.
See update below for explanation
remove the = in <%= flash.each.... %> Should just be <% flash.each.....%>
<% flash.each do |key, value| %>
<p><%= value %></p>
<% end %>
To keep it simple, when you want your ruby code to render something onto the page, use <%= %>, ex: to show the current time you would use <%= Time.now %>. For other things like settings variables in your view without text rendering on the page, use <% %>.
For example, say I want to assign a variable equal to the current time so I can use it below in the view, I would use <% time = Time.now %> then I can show that time on the page with <%= time %>.
<% time = Time.now %>
<p>The current time is <%= time %>.</p>

What's the cleanest way to add a class attribute to an html element in a view in rails

I'm writing some Rails code for a partial view, and I want it to only show a comment field if somebody is already logged onto a site here.
If the page is viewed by someone who isn't a member of the site yet, the shared/comment_not_logged_in fragment should be passed in.
However, I'm totally stumped as to why I can't run the same check to decide if the page should add the class attribute "missing_your_voice" to the enclosing div element here:
<li class="user_submission_form bubble comment_form <% "missing_your_voice" if not current_user %>">
<% if current_user %>
<%= image_tag(current_user.avatar(:comment), :class => "profile_pic") %>
<% form_for [parent, Comment.new] do |f| %>
<%= render "comments/form", :f => f %>
<% end %>
<% else %>
<%= render :partial => 'shared/comment_not_logged_in' %>
<% end %>
</li>
The same idiom, "missing_your_voice" if not current_user returns the string in irb, and also in the console debugger.
What am I doing wrong here?
You forgot an =. Replace <% by <%=, so that you get:
<%= "missing_your_voice" if not current_user %>
Remember that <% ... %> will only run Ruby code, but not display anything. Using <%= ... %> will run the code and display the result of the expression.
As molf already pointed out, there's a missing = on your view.
It should be <%=.
Other than that, be sure to make your controller method available to your view by calling helper_method in your controller.
Take a look on the documentation if needed.

Resources