NoMethodError in Discussions#new - ruby-on-rails

Can anyone help me to fix this?Thanks in advance!
I get the following error:
NoMethodError in Discussions#new
Showing C:/Users/punitha/aggregator/app/views/discussions/_form.html.erb where line #1 raised:
undefined method `discussions_index_path' for #<#<Class:0x4375758>:0x437d6d8>
Extracted source (around line #1):
1 <%= form_for #discussions do |f| %>
2 <% if #discussions.errors.any? %>
3 <div id="error_explanation">
4 <h2><%= pluralize(#discussions.errors.count, "error") %> prohibited
Trace of template inclusion: app/views/discussions/new.html.erb
Rails.root: C:/Users/punitha/aggregator
Application Trace | Framework Trace | Full Trace
app/views/discussions/_form.html.erb:1:in `_app_views_discussions__form_html_erb___1058370717_35436840'
app/views/discussions/new.html.erb:3:in `_app_views_discussions_new_html_erb__298093787_35389404'
And the file _form.html.erb is
<%= form_for #discussions do |f| %>
<% if #discussions.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#discussions.errors.count, "error") %> prohibited
this discussion from being saved:</h2>
<ul>
<% #discussions.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :source_url %><br>
<%= f.text_field :source_url %>
</p>
<p>
<%= f.label :discussion_id %><br>
<%= f.text_area :discussion_id %>
</p>
<p>
<%= f.label :discussion_msg %><br>
<%= f.text_area :discussion_msg %>
</p>
<p>
<%= f.label :discussion_type %><br>
<%= f.text_field :discussion_type %>
</p>
<p>
<%= f.label :discussion_link %><br>
<%= f.text_area :discussion_link %>
</p>
<p>
<%= f.label :discussion_thumb %><br>
<%= f.text_area :discussion_thumb %>
</p>
<p>
<%= f.label :discussion_permalink %><br>
<%= f.text_area :discussion_permalink %>
</p>
<p>
<%= f.label :discussion_likecount %><br>
<%= f.number_field :discussion_likecount%>
</p>
<p>
<%= f.label :comment %><br>
<%= f.text_area :comment%>
</p>
<p>
<%= f.label :source_id %><br>
<%= f.number_field :source_id%>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
And my app/views/discussions/new.html.erb is,
<h1>New discussion</h1>
<%= render 'form' %>
<%= link_to 'Back', discussions_path %>
routes.rb file
Aggregator::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
resources:discussions do
resources :comments
end
end

You're form_for is set to an array (#discussions) when it should be to a single instance (#discussion).
In your DiscussionsController's new method, ensure you have something along the following:
#discussion = Discussion.new
Then, in your form, change your current form_for expression to:
form_for #discussion
See if that clears your error.

First of all you should correct your typo mistake in routes.rb.
It should looks like (I have added a space between resources and :discussions):
Aggregator::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
resources :discussions do
resources :comments
end
end
As Craig Kaminsky said, it seems you are having #discussions as an array of discussion records.'
In your controller's action new, you should initialize your object as:
#discussion = Discussion.new
And you form should use same instance variable (#discussion)
<%= form_for #discussion do |f| %>
<% if #discussion.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#discussion.errors.count, "error") %> prohibited
...
<% end %>
...
...
<% end %>

Related

Why is post title not showing up?

So here it is:
For some reason this is not showing the title for my post.
However when I press show option and it goes to post's page you can see al the detail.
_form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
#show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<h1>Editing Post</h1>
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
edit.html.erb
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
I've tried changing it <%= form.text_area :name, id: :post_name %>
to string_area :name and also string_field but none of it is working.
In fact when I change it to the last 2, I get a method error if I go on the edit page. I'm still fairly new to rails and to ruby so I would greatly appreciate some help. I did try to google however and was unsuccessful, I guess I didn't know what to search for or maybe I worded it wrong.

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 routes form_for partial not working for new and edit actions - Rails 4

I used the generator to scaffold categories and questions.
my routes.rb
resources :categories do
resources :questions do
resources :choices, only: [:index]
end
end
my problem occurs when i try to add or edit a question.
here is my partial form, before you ask the relationship works ok.
<%= form_for [#question.category, #question] do |f| %>
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :question_type %><br>
<%= f.text_field :question_type %>
</div>
<div class="field">
<%= f.label :explanation %><br>
<%= f.text_field :explanation %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :category_id %><br>
<%= f.text_field :category_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
what must i change for it to work? for now i get,
undefined method `questions_path'
Try this
UPDATE
Controller
#category = Category.find(params[:category_id])
#question = #category.questions.new
_form
<%= form_for([#category, #question]) do |f| %>
Reference
You should pass parent and then build child for same for new object.
<%= form_for [#category, #category.questions.build] do |f| %>
To edit:
<%= form_for [#category, #category.questions.first_or_your_object] do |f| %>
Like this :
<% if #category.new_record? %>
<%= form_for [#category, #category.questions.build] do |f| %>
<% else %>
<%= form_for [#category, #category.questions.first_or_your_object] do |f|
<% else %>
Also add in your category model:
accepts_nested_attributes_for :questions
Add this line to your category.rb model file:
accepts_nested_attributes_for :questions
Also, in your controller:
#category = Category.find(params[:category_id])
#question = Question.new(category: #category)
and form:
<%= form_for([#category, #question]) do |f| %>

Date is not working in rails application with mongo id

I have create an rails application with mongoid but im facing one problem at DATE
I created an scaffold with a name "posting"
When im editing date it will updated....
I follow the instruction of Railscast #238 Mongoid here
there is my posting.rb file
class Posting
include Mongoid::Document
field :title
field :description
field :comments
field :published, :type => Date
end
this my _from.html.erb
<%= form_for(#posting) do |f| %>
<% if #posting.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#posting.errors.count, "error") %> prohibited this posting from being saved:</h2>
<ul>
<% #posting.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :published %><br>
<%= f.date_select :published %>
</div>
<div class="field">
<%= f.label :comments %><br>
<%= f.text_area :comments %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and finally my show.html.erb file
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #posting.title %>
</p>
<p>
<strong>Description:</strong>
<%= #posting.description %>
</p>
<p>
<strong>published:</strong>
<%= #posting.published %>
</p>
<p>
<strong>Comments:</strong>
<%= #posting.comments %>
</p>
<%= link_to 'Edit', edit_posting_path(#posting) %> |
<%= link_to 'Back', postings_path %>
What do you mean by not working? doesn't look like you have used published property in any of your views.
in your show.html.erb you are using
<%= f.date_select :pub %>
and in your show.html.erb you are using
<%= #posting.pub %>
However there is no property called pub in your Posting model. What you have there is called published
field :published, :type => Date
You either need to rename it in the model, or in the views to match.

How to check attribute value of current object in edit form?

I have a nested model. How to check attribute value of object(hotel) of neted model in edit form? I can't figure out how to write if/else statement in _hotels_fields.html.erb
edit.html.erb
<% provide(:title, "Edit trip") %>
<h1>Edit trip</h1>
<%= form_for(#trip) do |f| %>
<%= render 'fields_edit', f: f %>
<%= f.submit "Save changes" %>
<% end %>
_fields_edit.html.erb
<p>
<%= f.label :image %>
<%= f.file_field :image %>
</p>
<p>
<%= f.label :content %>
<%= f.text_area :content %>
</p>
<p>Hotel</p>
<%= f.fields_for :hotels do |builder| %>
<%= render 'hotels_fields', f: builder %>
<% end %>
_hotels_fields.html.erb
<% if #trip.hotels.name == "hotel" %>
<p>Render any text</p>
<% end %>
<fieldset>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
</p>
</fieldset>
In the context of _hotels_fields.erb.html one specific hotels (why plural here if you have only one?) is represented by f.object. Therefore this should work:
<% if f.object.name == 'hotel' %>
...
<% else %>
...
<% end %>

Resources