Rails 4 not rendering code - ruby-on-rails

I am trying to render a partial on a page in rails. For some reason the code is not being rendered into html. I have tried taking it out of the partial and just placing it in the profile page but still nothing. I am getting no errors and have restarted the server but still nothing. This is in development mode. Also all code except the message code works fine. Any help would be appreciated. Here is the code.
profile.html.erb
<% unless #pictures.nil? %>
<div id="container" style="width: 500px; height: 450px;">
<div id="slides">
<% #pictures.each do |picture| %>
<%= image_tag(picture.image.url(:thumbnail)) %>
<% end %>
<%= image_tag("left.png") %>
<%= image_tag("right.jpeg") %>
</div>
</div>
<% end %>
<% render 'message' %>
_message.html.erb
<% form_for #message, :url => messages_create_path do |f| %>
<% f.hidden_field :from, value: current_user.id %>
<% f.hidden_field :to, value: #user.id %>
<% f.text_area :message %><br />
<% f.submit "Send Message" %>
<% end %>

To make your form_for code block to render you need to use <%= tag as follows:
<%= form_for #message, :url => messages_create_path do |f| %>
<% f.hidden_field :from, value: current_user.id %>
<% f.hidden_field :to, value: #user.id %>
<% f.text_area :message %><br />
<% f.submit "Send Message" %>
<% end %>

To elaborate on vinodadhikary's answer, ERB has output tags and evaluation tags. To evaluate something you would use <% expression %>, and to output you would use <%= output.me %>. The earlier is usually used for flow control in templates, and outputs nothing. The output happens within after a decision is made. The latter is used to output stuff.

Related

How to insert new line breaks using form_for with collection_check_boxes

I use the following form_for at one point in my code to display a series of topics for users to select from.
<%= form_for #user do |f| %>
<%= f.collection_check_boxes(:topic_ids, Topic.all.sample(50).each, :id, :topic_name) %>
<%= f.submit %>
<% end %>
When it renders on the page, the boxes are all one-after-the-other. How can I separate them so that there is one check box per line?
from reference here
It is possibly to customize the way the elements will be shown by giving a block to the method as sample below from your code above
<%= form_for #user do |f| %>
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name do |b| %>
<%= b.label(class: "check_box") do %>
<%= b.check_box(class: "check_box") %>
<%= b.object.topic_name %></br>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
You may of course render HTML inside the block:
<%= form_for #user do |f| %>
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, : topic_name do |b| %>
<div class="my-checkbox-wrapper">
<%= b.label(class: "foo") do %>
<%= b.object.topic_name %>
<%= b.check_box(class: "bar") ></br>
<%end%>
<%= f.submit %>
<%end%>
You can have a look at this example
It is very broad question. In short, using CSS.
For example using Bootstrap 4:
<%= form_for #user do |f| %>
<div class="form-check">
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name, class: 'form-check-input' %>
</div>
<%= f.submit %>
<% end %>

Rails 5: Nil error in the first argument of a form

I'm quite new with rails so excuse my ignorance.
I have an homepage with a view of astronomic observations and I want to put in the same page the view of the outings that the observatory organize.
Since I can't append two views in the same page I tryed to pass the second view as a partial but I get an error
First argument in form cannot contain nil or be empty
So I though it was a problem of variables and tryed to pass the local variable of the partial but nothing changed.
So how do I pass a variable of #outing to the partial outings/_index.html.erb which will be visualized in the view of observative_sessions
I defined #outing=Outing.new in outings_controller which is the controller for the view outings
In app > views > observative_sessions I have the index.html.erb with
<div class="panel panel-default" id = "observative_sessions">
...
</div>
<%= render 'outings/index', :outings => #outing %>
In app > views > outings I put an _index.html.erb
<%= render 'outings/new_modal' %>
<div class="panel panel-default">
...
<tbody>
<% #outings.each do |outing| %>
...
<% end %>
</tbody>
The _new_modal.html.erb calls the _form.html.erb
<%= render 'outings/form' %>
And then the form
<%= bootstrap_form_for #outing do |f| %>
<%= f.date_field :day, label: 'Giorno:' %>
<%= f.text_field :location, label: 'Luogo:' %>
<%= f.time_field :time, label: 'Ora:' %>
<%= f.submit 'Aggiorna' %>
<% end %>
I get the error in the first line of the form with #outing.
If I put the view in a page alone everything work but not with the partial.
So what did I do wrong?
Thank you in advance for all the help.
In app > views > observative_sessions I have the index.html.erb with
<div class="panel panel-default" id = "observative_sessions">
...
</div>
<%= render partial: 'outings/index', locals: {outing: #outing} %>
In app > views > outings I put an _index.html.erb
<%= render partial: 'outings/new_modal',locals: {outing: outing} %>
<div class="panel panel-default">
...
<tbody>
<% #outings.each do |outing| %>
...
<% end %>
</tbody>
The _new_modal.html.erb calls the _form.html.erb
<%= render partial: 'outings/form', locals: {outing: outing} %>
Then in your form use the local outing variable:
<%= bootstrap_form_for outing do |f| %>
<%= f.date_field :day, label: 'Giorno:' %>
<%= f.text_field :location, label: 'Luogo:' %>
<%= f.time_field :time, label: 'Ora:' %>
<%= f.submit 'Aggiorna' %>
<% end %>
You need to pass #outing variable to all forms those are using #outing variable.
Change the line of rendering _new_modal.html.erb partial like this
<%= render 'outings/new_modal', outing: outings %>
Then change the line of rendering _form.html.erb like this
<%= render 'outings/form', outing: outing %>
And finally change the actual form like this
<%= bootstrap_form_for outing do |f| %>
<%= f.date_field :day, label: 'Giorno:' %>
<%= f.text_field :location, label: 'Luogo:' %>
<%= f.time_field :time, label: 'Ora:' %>
<%= f.submit 'Aggiorna' %>
<% end %>

blank comment entry with rails

I'm trying to add ability to add comments to projects within a "todo" type app and have run into a problem.
I've created a project with comments before and never run into this problem, but basically rails is drawing an empty comment on the project page.
I've tried a few if statements without any luck, does anyone see my problem ?
<% #project.comments.each do |comment| %>
<div class="commentBlock"><strong><%= comment.posted_by %> says:</strong>
<%=raw comment.comment %>
<small><i class="icon-remove"></i> <%= link_to 'Delete', [comment.project, comment],:confirm => 'Are you sure?',:method => :delete %></small></div>
<% end %>
<h3>Leave a comment</h3>
<%= form_for([#project, #project.comments.build]) do |f| %>
<div class="field">
<%= f.hidden_field :posted_by, :value => current_user.username %>
</div>
<div class="field">
<%= f.label :comment %><br />
<%= f.text_area :comment, :class => "tinymce" %><%= tinymce %>
</div>
<p><%= f.submit :class => 'btn' %></p>
<% end %>
Answer was an error in my controller for Projects, referenced #comment like so:
#comment = #project.comments.build(params[:comment]) by accident!!
Changed to:
#comment = #project.comments
And all works as it should :P
thanks for your help, bad "end of the day" error right there :P

Capturing a nil object and rendering a view

I seem to be unable to capture a nil response from my api call, when there are no results I get this error
undefined method `[]' for nil:NilClass
So if this happens I would like to show a message like 'No book found', so i have tried these
<% if #results.empty? %>
<% if #results.nil? %>
<% if #results.nil %>
but none of them capture the error
Controller
def results
results = book_search(params[:search])
#results = results
#book = Book.new
#book.author = results.first["artistName"]
#book.bookname = results.first["trackName"]
#book.image = results.first["artworkUrl100"].gsub("100x100-75.jpg", "200x200-75.jpg")
#book.description = results.first["description"]
end
View
<div class="container">
<div class="row">
<div class="span8 offset2">
<% if #results.nil? %>
<h1 class="resultTitle">sorry we can't find anything for that book.</h1>
<%= link_to "Back To Search", root_path, :class => 'backButton' %>
<% end %>
<%= #results.first["trackName"] %> <br>
<img src =<%= #results.first["artworkUrl100"] %>> <br>
<%= #results.first["artistName"] %> <br>
<%= truncate(remove_tags(#results.first["description"]), :length => 250) %> <br>
<%= form_for #book do |f| %>
<%= f.label :category_id, "Category" %>
<%= f.collection_select(:category_id, Category.all, :id, :name, :prompt => 'Please select a Category') %>
<%= f.hidden_field :author %>
<%= f.hidden_field :bookname %>
<%= f.hidden_field :image %>
<%= f.hidden_field :description %>
<%= f.submit 'Save book' %>
<% end %>
</div>
</div>
</div>
Any ideas on what it could be
Thanks
The latter version will return true if #results is nil. You have some other error elsewhere.
If the error still shows up, then you access results even when #results.nil? returns true. More specifically take a look at the places where you call the square bracket operator [] as the error is caused by one of them.
EDIT: after discussion in chat we have discovered the problem. First in the conroller book_search returns an empty array and then, when invoking results.first["artistName"] this causes the error, because results.first is nil. So to fix that we added the following to the controller:
def results
results = book_search(params[:search])
#results = results
unless #results.empty?
#book = Book.new
#book.author = results.first["artistName"]
#book.bookname = results.first["trackName"]
#book.image = results.first["artworkUrl100"].gsub("100x100-75.jpg", "200x200-75.jpg")
#book.description = results.first["description"]
end
end
I.e. a check to only create a new book and set its properties if a book was found. Now in the view, a check should be added to only render the rest of the view(the part that invokes [] operator on #results if results is not empty. This is achieved using an else statement:
<div class="container">
<div class="row">
<div class="span8 offset2">
<% if #results.empty? %>
<h1 class="resultTitle">sorry we can't find anything for that book.</h1>
<%= link_to "Back To Search", root_path, :class => 'backButton' %>
<% else %> <--- I've putted an else instead of end here!!!
<%= #results.first["trackName"] %> <br>
<img src =<%= #results.first["artworkUrl100"] %>> <br>
<%= #results.first["artistName"] %> <br>
<%= truncate(remove_tags(#results.first["description"]), :length => 250) %> <br>
<%= form_for #book do |f| %>
<%= f.label :category_id, "Category" %>
<%= f.collection_select(:category_id, Category.all, :id, :name, :prompt => 'Please select a Category') %>
<%= f.hidden_field :author %>
<%= f.hidden_field :bookname %>
<%= f.hidden_field :image %>
<%= f.hidden_field :description %>
<%= f.submit 'Save book' %>
<% end %>
<%end %> <--- Added an end for the if-else here.
</div>
</div>
</div>
So I have added an else and an end that I have indicated in the code. Why do you need to do that? Well because even after performing the check and displaying the message, the rest of the page was still evaluated and this bit: %= #results.first["trackName"] %> <br> was causing the error. You should not try to call the [] operator if you know #results is empty. The new version only displays the remaining part of the page in the else case i.e. if #results is not empty.
Hope this makes it clear.

" undefined method `enumerable_enumerator_path' " error

I'm using basic scaffold structure. What I need, is to add 'moderate' action and view by changing published to true. In my idea, on moderate.html I should get the list of all unpublished entries with the ability to change and save their parameters.
Here are parts of my code:
#names_controller.rb
def moderate
#name = Name.find(:all, :conditions => {:published => false} )
respond_to do |format|
format.html
format.xml
end
end
#moderate.html.erb
<% form_for #name.each do |f| %>
<%= f.error_messages %>
<%= f.text_field :which %>
<%= f.text_field :what %>
<%= f.check_box :published %>
<%= f.submit %>
</p>
<% end %>
Instead I'm getting this error:
NoMethodError in Names#moderate
Showing app/views/names/moderate.html.erb where line #1 raised:
undefined method `enumerable_enumerator_path' for #<ActionView::Base:0x1042c3e90>
Extracted source (around line #1)
So, can you help to newbie please?
ruby 1.8.7 (2009-06-12 patchlevel 174)
[universal-darwin10.0] Rails 2.3.5
If you want to update each name in a separate form, then all you need to do is move the loop above form_for:
<% #name.each do |n| %>
<% form_for n do |f| %>
<%= f.error_messages %>
<%= f.text_field :which %>
<%= f.text_field :what %>
<%= f.check_box :published %>
<%= f.submit %>
</p>
<% end %>
<% end %>
But if you'd like to do it all in one submit (a single form) then I guess you can't use form_for. I'd use form_tag to create a custom form to update multiple instances. This should work both for create and edit form:
<%= form_tag moderate_names_path do %>
<% #names.each do |name| %>
<fieldset>
<%= fields_for "name[#{name.id}]", name do |name_fields| %>
<p><%=name_fields.label(:this)%>: <br /><%= name_fields.text_field :this %></p>
<p><%=name_fields.label(:that)%>: <br /><%= name_fields.text_field :that %></p>
<p><%= name_fields.check_box :published %> <%=name_fields.label(:published)%></p>
<% end %>
</fieldset>
<br />
<% end %>
<%= submit_tag %>
<% end %>
NOTICE: I changed #name to #names in the second example

Resources