rails form_for nested data doesnt show up - ruby-on-rails

New to rails and getting confused on how to handle this. I am using rails 4. I am very stuck here and will try to talk through this problem.
I have a listings page which I am trying to add tags too. In my view (listings/new.html.erb) I have the following:
<h1> POST A NEW LISTING </h>
<% if current_user.nil? %>
<h2>You must be logged in to view this page </h2>
<% else %>
<%= form_for [#user, #listing] do |f| %>
<%= f.label :title, 'Title' %> <br />
<%= f.text_field :title %>
<%= f.label :general_info, 'General Information' %> <br />
<%= f.text_area :general_info %>
<%= f.label :included, 'Included' %> <br />
<%= f.text_field :included %>
<%= f.label :length, 'Length' %> <br />
<%= f.text_field :length %>
<%= f.label :price, 'Price' %> <br />
<%= f.text_field :price %>
<% fields_for #tagging do |u| %>
<%= u.label :tag, 'Tag' %> <br />
<%= u.text_field :tag %>
<% end %>
<%= f.submit "submit" %>
<% end %>
<% end %>
The form works correctly for putting in the listing, but the tagging form options do not even appear to allow for content.
my listings_conroller #new looks like this:
def new
if (!current_user.nil?)
#user = User.find(current_user.id)
#listing = #user.listings.build
#tagging = #listing.taggings.build
end
end
I want to be able to create a new listing with this form that also populates the database for the tags and I am very unsure on how to do this. I hope this is enough information, but if needed I have all the code here: https://bitbucket.org/r-s/ath/src . Very stuck on this any help would be appreciated.

Change it to:
<%= f.fields_for #tagging do |u| %>
Note the =.

You forgot to use builder:
<%= f.fields_for #tagging do |u| %>

Related

Nested Forms: How to get a "this"?

I use nested forms and would like to get some kind of "this" in it:
The View looks like this:
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<% f.fields_for :assignments do |builder| %>
<%= builder.label :count %>
<%= builder.text_field :count %>
<%= #a.fetch(this.id) %>
<% end %>
The #a is an array with the caption of the assignment. So how can I access the current assignment? Or would this code only display a form for one assignment?
Thanks for your help!
Via #object applied to your builder:
builder.object.id

formatting a form inside my rails app

I have a rails app with form inside of it. my css file is all ready. i just don't know how to apply the css form classes and ids also to the fields inside of my create action page where
my form_for lives?
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p class="button"><%= f.submit %></p>
<% end %>
my css styles for the form tag and for its fiels as below:
id="phx-signup-form"
e-mail field css style:
class="email-input"
Try
<%= form_for #user, :html => { :id => 'phx-signup-form' } do |f| %>
and
<%= f.text_field :email, :class => 'email-input' %>
You can read more references on http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
A good place to start is the Rails API documentation.
form_for(#user, html: { id: 'phx-signup-form' })
should put you on the right path.
Your form automatically takes id as new_user if your form is running as new form and takes id as edit_user if your form is running as edit form. So you use id="new_user" or id="edit_user" instead of id="phx-signup-form" in your css depending on condition.
For your form and <%= f.text_field :email, "", :class => 'email-input' %> for emai field. Just try it. It may solve your problem.

Rails: Errors close to particular fields in forms

I am trying to add some errors to a form close to the field that caused the error and here is how I am doing this:
<%= lesson_form.text_field :description %><br />
<% unless #lesson.errors[:description].blank? %>
<span id="error_explanation">
Description <%= #lesson.errors[:description].join(", ") %>
</span>
<% end -%>
<%= lesson_form.label :category %>
<%= lesson_form.text_field :category %><br />
<% unless #lesson.errors[:category].blank? %>
<span id="error_explanation">
Category <%= #lesson.errors[:category].join(", ") %>
</span>
<% end -%>
I would like to know if there is a better an non repetitive way of doing this. As you see I am repeating the same unless errors... for each of the fields.
Use a helper method:
def errors_for(model, attribute)
if model.errors[attribute].present?
content_tag :span, :class => 'error_explanation' do
model.errors[attribute].join(", ")
end
end
end
And in view:
<%= lesson_form.text_field :description %><br />
<%= errors_for #lesson, :description %>
<%= lesson_form.label :category %>
<%= lesson_form.text_field :category %><br />
<%= errors_for #lesson, :category %>
<% end %>
Or you could use simple_form which will do it all for you like this:
<%= simple_form_for #lesson do |f| %>
<%= f.input :description %>
<%= f.input :category %>
<%= f.button :submit %>
<% end %>
And if you use simple_form and haml, things look a bit neater:
= simple_form_for #lesson do |f|
= f.input :description
= f.input :category
= f.button :submit
The above will show the error next to the field and will detect type of the attribute and show the appropriate input box (eg text, password, checkbox etc.), all with one simple line f.input.

Associated model is not saving data when page is refreshed

Rails 3.1 RC4
I have a 1:1 association between User and Profile.
When I submit the new profile form, the data I've entered is displayed just fine (see screenshot: http://i.imgur.com/fY8YU.png), but when I refresh it the data is instantly wiped.
Could anyone tell me what is causing this?
Here's the submit form:
<%= form_for([#user, #user.build_profile]) do |f| %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :picture %><br />
<%= f.text_field :picture %>
</div>
<div class="field">
<%= f.radio_button(:sex, "male") %>
<%= f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female") %>
<%= f.label(:sex, "Female") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here's the users_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/users_controller.rb
Here's the profiles_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/profiles_controller.rb
I'm not sure I agree with your approach. Why don't you do something like this:
In models/user.rb:
accepts_nested_attributes_for :profile
In controllers/users_controller.rb:
def new
#user = User.new
#user.build_profile
end
In views/users/_form.html.erb:
<%= form_for #user do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :profile do |pf| %>
<%= pf.text_field :some_profile_field %>
<% end -%>
<%- end -%>
This isn't copied or tested, but it should work. On saving your user, the profile fields are sent along and validated with the user fields and are re-rendered when rendering the form again after a save error. This way you will keep full control over your form and its contents with minimal effort.

How do you call a from partial from different model in a layout/header

In Rails how do I call a form from another model in any given layout? I have a login form I want to put in the header of every page. I created a partial with the following in it:
<% form_for(#user_session) do |f| %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username, :class=>'' %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<%= f.check_box :remember_me %><%= f.label :remember_me %><br />
<br />
<p>
<%= f.submit 'Login' %>
</p>
<% end %>
then tried calling that partial in my header and that doesn't seem to work.
<%= render :partial => 'user_sessions/login' %>
I get a "Called id for nil" error
<%= render :partial=> 'user_sessions/login' :layout => false%>
Use this
I think you save your partial with '_login.rhtml' like ?
Why not just <%= render #user_session %> ?
Is the #user_session variable set up within all of your controller actions?
Got the answer from jmesserer over at railsforum, just needed to change:
<% form_for(#user_session) do |f| %>
to
<% form_for UserSession.new do |f| %>

Resources