NameError in User#new - ruby-on-rails

I have a model named User and am referencing it in the form. The form is as follows:
<%= form_with(model: user, local: true) do |f| %>
<p>Who is the participant?</p>
<div class="field">
<%= f.label :person %>
<%= f.text_field :input %>
</div>
<p>What gifts would they like?</p>
<div class="field">
<%= f.label :gifts %>
<%= f.text_field :gifttext %>
</div>
<p>Who, if anyone, is their spouse? If they don't have one you can leave
this field blank</p>
<div class="field">
<%= f.label :person %>
<%= f.text_field :input %>
</div>
<p>What gifts would they like? If there is no spouse then you can leave this
field blank.</p>
<div class="field">
<%= f.label :gifts %>
<%= f.text_field :gifttext %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<%= link_to 'Done', user_results_path(#user) %>
<% end %>
Then I get this error:
NameError in User#new Showing C:/Sites/sstwo/app/views/user/_form.html.erb where line #1 raised:
undefined local variable or method `user' for
#<#<Class:0x5590668>:0x41b5ff0> Did you mean? #user Extracted source (around line #1): 1 2 3 4 5 6
I'm sure it's a quick fix so I'm willing to learn. If any more details are needed please let me know.

In the first line, you're using user, should be #user
form_with(model: #user, local: true)
Source: https://m.patrikonrails.com/rails-5-1s-form-with-vs-old-form-helpers-3a5f72a8c78a

Related

Nested route values not populating edit Rails

I have a belongs_to relationship between a map and a row. A map has_many rows.
For some reason, when I try and edit a row, even when it is populated in my database, it is not showing the values when I try to edit this row. Why would this be? Below is the _form.html.erb and the edit.html.erb file.
Edit.html.erb
<%= render 'form', row: #row %>
_form.html.erb
<%= form_for [#map, #map.rows.build], method: :post, url: map_rows_path do |form| %>
<div class="field">
<%= form.label :timeframe %>
<%= form.text_field :timeframe, id: :timeframe %>
</div>
<div class="field">
<%= form.label :standards %>
<%= form.text_field :standards %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_field :content %>
</div>
<div class="field">
<%= form.label :skills %>
<%= form.text_field :skills %>
</div>
<div class="field">
<%= form.label :resources %>
<%= form.text_field :resources %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
If I try Row.last in the rails console it confirms that this row is populated with data. I am assuming this has something to do with the way the form is setup however I am not familiar with these relationships. How can this be fixed?
it seems you are passing row to form where <%= render 'form', row: #row %> but in Actual edit form you are using a variable, #map and building a blank row object by saying #map.rows.build, hence the row object will be a new object with out any values.
Based on your code change it to following and see. ( I am assuming you are setting up #map object in your edit action in controller)
<%= form_for [#map, row], method: :post, url: map_rows_path do |form| %>

How do i write my form code for 3rd nested model in Rails 5?

I'm working on an app where i nested the 3 model
resources :genres do
resources :stories do
resources :episodes
end
end
Meanwhile, i've been able to write the form for genres and stories as in
<%= form_for(genre) do |f| %>
<% if genre.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(genre.errors.count, "error") %> prohibited this genre from being saved:</h2>
<ul>
<% genre.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and stories
<%= form_for([#genre, #genre.stories.build]) do |f| %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :summary %>
<%= f.text_area :summary %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Can someone pls tell me how to write the form for episodes...
When i wrote this
<%= form_for([#genre.stories, #genre.#stories.episodes.build]) do |f| %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I got this error
syntax error, unexpected tIVAR, expecting '(' ...#genre.stories, #genre.#stories.episodes.build]) do |f| #out... ... ^ C:/Users/Adegbite/Documents/Apps/twiliste2/app/views/episodes/_form.html.erb:1: syntax error, unexpected ']', expecting keyword_end ...#genre.#stories.episodes.build]) do |f| #output_buffer.safe_... ... ^ C:/Users/Adegbite/Documents/Apps/twiliste2/app/views/episodes/_form.html.erb:16: syntax error, unexpected keyword_ensure, expecting end-of-input
Thanks.
This:
<%= form_for([#genre.stories, #genre.#stories.episodes.build]) do |f| %>
Should be more like:
<%= form_for([#genre, #story, #story.episodes.build]) do |f| %>
Of course, you'll have to set #genre and #story appropriately in the new action of your EpisodesController.
BTW, #genre.#stories makes no sense and is not valid. #stories is an instance variable which, presumably, contains (or would contain) one or more stories. .stories is a method call on an instance of Genre. .#stories is nothing.

Nested_form_for with field_for not working

my view is
<h3> Register New Items </h3>
<div class="row">
<div class="span2 offset4">
<%= nested_form_for #item_category, :url => items_path, :method => :post, :html => { :class => "item_category"} do |f| %>
<div><%= f.label :item_name %>
<%= f.text_field :item_name %></div>
</br>
<div>
<%= f.fields_for :item_companies do |f| %>
<%= f.label :company_name %>
<%= f.text_field :company_name %>
</div>
</br>
<%= f.fields_for :item_weights do |f| %>
<div>
<%= f.label :weight %>
<%= f.text_field :weight %>
</div>
</br>
<div>
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<%end%>
<%end%>
<div><%= f.submit "Submit" %></div>
<% end %>
</div>
and controller
def new
#item_category = ItemCategory.new
item_company = #item_category.item_companies.build
item_company.item_weights.build
end
and when I have applied debugger in create action and I saw value of params[:item_category] and this is generated {"item_name"=>"iname", "item_companies_attributes"=>{"0"=>{"company_name"=>"cname", "item_weights_attributes"=>{"0"=>{"weight"=>"20kg", "price"=>"100"}}}}}
ItemCategory.new(params[:item_category])
generated error
ActiveModel::MassAssignmentSecurity::Error Exception: Can't mass-assign protected attributes: item_companies_attributes.
Where i am wrong and how to save value in three tables using this.
It seems that you forgot to add this piece of code in your ItemCategory model:
attr_accessible :item_companies_attributes
which will allow you to set these attributes.

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.

Rails: best way to validate and display error messages for fields not in model

In my Rails 3 application I have a typical 'Email this to a friend' form that has yours/friend's email and name fields. Each of them must be validated and error messages must be displayed, in a fashion similar to ActiveRecord forms:
<%= form_tag %>
<div class="fields">
<%= label_tag :friends_name %>
<%= text_field_tag :friends_name, params[:friends_name] %>
<%= label_tag :friends_email %>
<%= text_field_tag :friends_email, params[:friends_email] %>
</div>
<div class="fields">
<%= label_tag :your_name %>
<%= text_field_tag :your_name, params[:your_name] %>
<%= label_tag :your_email %>
<%= text_field_tag :your_email, params[:your_email] %>
</div>
<div class="fields">
<%= label_tag :message %>
<%= text_area_tag :message, params[:message] %>
</div>
<%= submit_tag 'Send'%>
</form>
What would be the 'Rails way' of doing this?
I found the answer in the episode #219 - Active Model on railscasts.com

Resources