Rendering error messages not working on just one page - ruby-on-rails

I can't get error messages to display on this page. They display fine for any other page but not this one
mod_approval.index.html.erb
<% #check_category.each do |category| %>
<%= form_for([#guide, Guide.friendly.find(#guide.id).categories.new], url: guide_mod_panel_approve_category_path, method: :post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= hidden_field_tag :check_id, category.id %>
<%= f.hidden_field :name, :value => category.name %>
<%= f.submit "Approve" %>
<% end %>
<% end %>
mod_approval_controller
def mod_add_category
#guide = Guide.friendly.find(params[:guide_id])
#check_category = CheckCategory.where(guide_id: #guide.id).all
#category = Guide.friendly.find(#guide.id).categories.new(category_params)
if #category.save
flash[:success] = "Game category added succesfully!"
redirect_to guide_mod_panel_mod_approval_index_path(#guide)
else
render 'mod_approval/index'
end
end
routes
match '/guides/:guide_id/mod-panel/approve/category' => 'mod_approval#mod_add_category', :via => :post, as: :guide_mod_panel_approve_category
match '/guides/:guide_id/mod-panel/approve' => 'mod_approval#index', :via => :get, as: :guide_mod_panel_mod_approval_index
Not too sure why they aren't rendering I tried changing <%= render 'shared/error_messages', object: f.object %> to <%= render partial: 'shared/error_messages', object: f.object %> but that gives the error
undefined local variable or method `object' for #<#<Class:0x007ffdbcd1c3a8>:0x007ffdbcbdd320>
on this line <% if object.errors.any? %>
This error rendering setup was made from Michael Hartls rails tutorial and as I said I works fine for every other form but this one.

The reason is that when you render the form, you instantiate a new Category:
form_for([#guide, Guide.friendly.find(#guide.id).categories.new],
You need to give form_for the Category instance that has the errors (#category from your controller). So I would change your form to this:
form_for([#guide, #category],
And then in your #new method make sure you set it up:
#category = #guide.categories.build

Related

passing variable to simple form symbol

this is my _form_item partial in which I used symbol :order_item
<%= simple_form_for :order_item do |f| %>
.....
<% end %>
here is my view in which I want to render that partial:
<%= content_tag_for :tr , #order.order_items do |i| %>
<div class="hide">
<%= render :partial => "form_item" %>
</div>
<% end %>
How can I pass "i" object to :order_item?
UPDATE:
I prefer to keep it ":order_item" instead of changing it to something like "foo".
You should have:
<%= render partial: 'form_item', locals: {order_item: i} %>
or a shorthand:
<%= render 'form_item', order_item: i %>
And in your form_item partial you should have:
<%= simple_form_for order_item do |f| %>
...
Just do
<%= simple_form_for item do |f| %>
.
.
<% end %>
and render the partial via
<%= render :partial => 'form_item', :locals => { :item => i } %>
You can pass i as a locals to the partial like follows:
<%= render :partial => "form_item", :i => i %>
and you'll have i available in the _form_item partial.
Update:
Local variables passed to partials are variables and not symbols. You could keep the name order_item like follows:
<%= render :partial => "form_item", :order_item => i %>
And update your order_item partial as follows:
<%= simple_form_for order_item do |f| %>
.....
<% end %>
I think I understand why you do not want to change :order_item to just order_item. I think you are going to have to update your other calls to pass in local variable order_item where ever you are making a call to this partial.

adding an if statement within brakets in ruby on rails

I have the following in my view .html.erb:
<%= form_tag :action=>"edit", :id => #product.id do %>
I want to basically only add the id if #product exists (not null i suppose) so i want to do something along the lines of:
<%= form_tag :action=>"edit" if #product print",:id => #product.id" end do %>
I know the above is wrong code but that's the idea behind it, not sure if i can embed that within the <%= tag.
Thanks
try
<%= form_tag :action=>"edit", :id => (#product.id if #product) do %>
<%
url_options = { :action => "edit" }
html_options = {}
html_options[:id] = #product.id if #product
%>
<%= form_tag url_options, html_options do %>
<% .... %>
<% end %>
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag
http://apidock.com/rails/v3.2.8/ActionView/Helpers/FormTagHelper/form_tag
This should work:
<% if #product %>
<%= form_tag :action=>"edit",:id => #product.id" do %>
<% else %>
<%= form_tag :action=>"edit" do %>
<% end %>
You can probably embed the conditional code in the <%= ... %> tag if you use eval but the above might be more clear!

retrieving multiple model objects from rails partial form

I have the below form working as a partial, i'm trying to do a partial call for a Contractors models and i want to also pass the current page's model id which is a quote id.
Its failing on this line <%= hidden_field_tag :quote_id, #quote.id %> 'called id for nil'
I've tried creating a manual route and putting the search on a seperate method, but then i get a template error so i'm just leaving it in the index method for now.
Form in show.html.erb:
<%= form_tag quotes_path, :method => 'get', :id => "contractors_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<br><br><br>
<div id="contractors"><%= render 'contractors' %></div>
<% end %>
_contractors.html.erb
<table>
<% #contractors.each do | contractor | %>
<tr>
<td><%= contractor.firstname %></td>
<td>
<%= form_tag (quote_add_contractor_path) do %>
<%= hidden_field_tag :quote_id, #quote.id %>
<%= hidden_field_tag :contractor_id, contractor.id %>
<%= submit_tag "Add" %>
<% end %>
</td>
</tr>
<% end %>
</table>
Index.js.erb
$("#contractors").html("<%= escape_javascript(render :partial => "contractors") %>");
Controller:
def index
#quotes = Quote.all
#contractors = Contractor.search(params[:search])
end
def add_contractor
#quote = Quote.find(params[:quote_id])
#contractor = Contractor.find(params[:contractor_id])
#quote.contractors << #contractor
if #quote.save
redirect_to #quote, notice: "contractor was added"
else
render :show, notice: "Sorry, something went aweful"
end
end
In index.js.erb you render the partial contractors but you do not set the #quote instance variable in your index action of the controller. That is why you are getting this failure. Try to add #quote = # Some logic here to your index action.

Rendering form_for error messages outside of form div for rails?

How do I render form_for error messages outside of the form_for div?
This is what I have now:
<div id="editUser_form", class="round">
<h1>Edit user</h1>
<%= form_for #user, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
...
<% end %>
</div>
However, I want it to look more like this:
<%= render 'shared/error_messages', :object => f.object %>
<div id="editUser_form", class="round">
<h1>Edit user</h1>
<%= form_for #user, :html => { :multipart => true } do |f| %>
...
<% end %>
</div>
The code above gives me an error because :object => f.object must be part of the form. Is there a way to pass the f.object to the shared/error_messages?
Thanks!
The object is #user, so you can do this:
<%= render 'shared/error_messages', :object => #user %>
Anywhere that #user is defined, as long as the partial doesn't rely on anything else in the form object.
f.object is just the object passed as the first argument to form_for, so:
<%= render 'shared/error_messages', :object => #user %>

Rendering form partials from other controllers

I have a few shared partials that I can render from any controller fine however I am having a bit of trouble rendering form partials from another controller. I am wanting to be able to add notes to my contacts
In my contacts/show.html.erb i have the following
<% render :partial => "notes/form", :note => Note.new %>
In my notes/_form.html.erb i have the following
<%= form_for #note do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<p>
<%= f.label :content %><br />
<%= f.text_field :content %>
</p>
<p>
<%= f.label :contact_id %><br />
<%= f.number_field :contact_id %>
</p>
<p><%= f.submit %></p>
<% end %>
However I get the error:
Showing /Applications/Rails/apps/saas31/app/views/notes/_form.html.erb where line #1 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for #note do |f| %>
2: <%= render 'shared/error_messages', :object => f.object %>
I'm starting to get the hang of rails but having a few small frustrating problems as to be expected when learning anything new i suppose. Anyone have any ideas?
Your local variables should be passed through in a locals hash.
<% render :partial => "notes/form", :locals => {:note => Note.new} %>
Read section 3.4.4 here.
Your partial also shouldn't use instance variables, change the following:
<%= form_for #note do |f| %>
to:
<%= form_for note do |f| %>
edit
If you want to use an instance variable, you can do the following:
<% render :partial => "notes/form", :locals => {:note => #note} %>
Ran into the same issue and this post was helpful in solving. Adding my notes and hopefully it will help someone else :)
I had a a Users controller and a _form.html.erb that rendered fine when I would access the /users/new page. I was trying to render the form as a partial from my /layouts/application.html.erb, as I wanted to give users the ability to create a new user from any page.
I ended up creating a new method (new_user) in application_helper.rb. Here is the code:
def new_user
User.new
end
I then render the partial from application.html.erb with:
<%= render :partial => 'users/form', :locals => {:user => new_user} %>

Resources