Why am I getting this error with my RoR app? - ruby-on-rails

I get this error:
NoMethodError in Videos#new
Showing /rubyprograms/dreamstill/app/views/videos/new.html.erb where line #1 raised:
undefined method `videos_path' for #<#<Class:0x10398f8d8>:0x10398dbc8>
I have one Video model and a videos controller with a new and create method. My routes.db file has root :to => "videos#new". I have one view new.html.erb with this code:
<%= form_for(#video) do |f| %>
<% if #video.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#video.errors.count, "error") %> prohibited this video from being saved:</h2>
<ul>
<% #video.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :video_url %><br />
<%= f.text_field :video_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And my controller has this:
def new
#video = Video.new
end
def create
#video = Video.new(params[:video])
respond_to do |format|
if #article.save
format.html #{ redirect_to(#video, :notice => 'Article was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
This is all that's in my routes file:
Dreamstill::Application.routes.draw do
root :to => "videos#new"
end

your routes should be
Dreamstill::Application.routes.draw do
root :to => "videos#new"
resources :videos
end

Related

How to retrieve id sent from a link_to in rails

I am trying to to edit a form through a reveal-modal. So i have multiple posts.
When I click the link 'Edit' it will reveal a div and display a form inside that is repopulated with the information. However, I don't know how to pass the post id into the form it will know which post to edit.
If i try to render the partial form I get this error:
undefined method `model_name' for Post::ActiveRecord_Relation:Class
All my information are on a controller view that is called Dashboard
controllers/dashboards_controller.rb
class DashboardsController < ApplicationController
def index
#profile = Profile.find(current_user)
#profileAll = Profile.all
#post = Post.all
end
def show
end
def edit
#profile =Profile.find(current_user)
#post = #profile.post.find(params[:id])
end
end
controllers/posts_controller.rb
class PostsController < ApplicationController
def create
#profile = Profile.find(current_user)
#post = #profile.post.create(post_params)
redirect_to dashboards_path(current_user)
end
def destroy
#post =Profile.find(params[:profile_id])
#post = profile.post.find(params[:id])
#post.destroy
redirect_to dashboards_path(current_user)
end
def show
#profile =Profile.find(current_user)
#post = #profile.post.find(params[:id])
end
def edit
#profile =Profile.find(current_user)
#post = #profile.post.find(params[:id])
end
def update
#post = profile.post.find(params[:id])
if #post.update(post_params)
redirect_to dashboards_path(current_user)
else
render 'edit'
end
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
posts/_form2.html.erb
<%= form_for #post do |f| %>
<% 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :content %><br>
<%= f.text_area :content %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
routes.rb
Rails.application.routes.draw do
get 'homepages/index'
resources 'dashboards' do
resources 'posts'
end
#get 'users/sign_in'
resources 'profiles' do
resources 'posts'
end
devise_for :users, :controller => {:registrations => "users/registrations"}
devise_scope :user do
root :to => 'devise/sessions#new'
end
end
Dashboards/index.html.erb
<% #post.each do |post| %>
<div class="panel">
<%= #profileAll.find(post.profile_id).name %>
<hr>
<h5><%= post.title %></h5>
<p><%= post.content %></p>
<%# link_to "Edit", edit_dashboard_post_path(current_user,[post.profile, post]) %>
<%= link_to "Edit", {id: #post},'data-reveal-id' => 'edit-post' %>
</div>
<% end %>
<div id="edit-post" class="reveal-modal" data-reveal>
<%= render 'posts/form2' %>
<a class="close-reveal-modal">×</a>
</div>
Well, it simply does not work that way. First, you have in your controller #post = Post.all and within your _form2.html.erb you want to render form for a collection. That should be:
# index.html.erb
<div id="edit-post" class="reveal-modal" data-reveal>
<%= render :partial => 'posts/form2', collection: #posts %>
<a class="close-reveal-modal">×</a>
</div>
#dashboard controller:
def index
#...
#posts = Post.all
end
_form2.html.erb:
Every #post object instance has to be replaced with form2. Read more here: http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html about rendering a collection.
However, consider a case when you have hundreds of posts. Then, for each post, a partial is rendered. That would be very inefficient so instead, consider an asynchronous request which will load only one post the user requested.

Couldn't find Post without an ID / ActiveRecord::RecordNotFound

I know this is a very basic solution however, I am just not seeing it right now. I am getting a 'Couldn't find Post without an ID' error within CommentsController#create.
I created a 'New Comment' button under the post which should then redirect to the comment form. From there once a user inputs their comments and clicks the 'Create Comment' button the comment should be displayed under the original post. Thank you in advance.
Comments Controller
Class CommentsController < ApplicationController
before_filter :authenticate_user!
def new
#comment = Comment.new
end
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(params[:comment].permit(:commenter, :body))
respond_to do |format|
if #comment.save
format.html { redirect_to #post, notice: 'Comment was successfully created.' }
format.json { render json: #comment, status: :created, location: #comment }
else
format.html { render action: "new" }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
def destroy
#post = Post.find(params[:post_id])
#comment = #post.comments.find(params[:id])
#comment.destroy
redirect_to post_path(#post)
end
def show
#comment = Comment.new
end
end
Post/Show
<div class="row">
<div class="col-md-offset-4 col-med-8">
<div class="panel panel-default">
<div class="panel-heading center">
<% if #post.image.url %>
<%= image_tag #post.image.url(:medium) %>
<% elsif #post.video.url %>
<%= video_tag #post.video.url(:medium), controls: true, type: "video/mp4" %>
<% end %></br>
<p>
<strong>Likes:</strong>
<%= #post.get_likes.size%>
</p>
<%=link_to image_tag('like.jpg', :border => 0), likes_post_path(#post) %>
<%=link_to image_tag('unlike.jpg', :border => 0), dislikes_post_path(#post) %>
</div>
<div class="panel-body">
<p><%= #post.description %></p>
<% user = #post.user %>
<p><strong><%= link_to(user.name, user_path(user)) if #post.user %></strong></p>
<%= link_to 'New Comment', new_comment_path, class: "btn btn-danger btn-sm active" %></br>
<br><% if #post.user == current_user %>
<%= link_to edit_post_path(#post) do %>
<span class="glyphicon glyphicon-edit"></span>
Edit
<% end %>
<% end %>
<%= link_to 'Back', posts_path %>
</div>
</div>
</div>
Comments / _form
<%= form_for [#post, Comment.new] do |f| %>
<% if #comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit class: "btn btn-danger btn-sm" %>
</div>
<% end %>
Routes
resources :comments
resources :posts do
resources :comments
end
Comments / Show
<p id="notice"><%= notice %></p>
<p>
<strong>Post:</strong>
<%= #comment.post_id %>
</p>
<p>
<strong>Body:</strong>
<%= #comment.body %>
</p>
<%= link_to 'Edit', edit_comment_path(#comment) %> |
<%= link_to 'Back', comments_path %>
Rake Routes
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
I recommend making the following changes:
Routes:
Remove the first resources :comments. Leave the following:
resources :posts do
resources :comments
end
Posts/Show view:
You should be using new_post_comment_path instead of new_comment_path. Run rake routes to see why.
CommentsController#new:
Define #post in your new action:
def new
#post = Post.find(params[:post_id])
#comment = Comment.new
end
Finally, in Comments/_form:
Change <%= form_for [#post, Comment.new] do |f| %> to <%= form_for [#post, #comment] do |f| %>. Although I believe <%= form_for #comment do |f| %> should work.
I recommend going through the Rails Guides for additional information and explanations.
I believe that in def new of the comments controller, you also need to set #post, not just #comment

I receive an error that "No Route Matches" {...id=>nil} *Wicked*

I have seen a lot of similar routing issues and id=>nil posts, but none of the solutions have resolved my error.
First here is the entire error:
ActionController::UrlGenerationError in ProfileSteps#personal
Showing ...profile_steps/personal.html.erb where line #1 raised:
No route matches {:action=>"show", :controller=>"profile_steps", :id=>nil
I am using Wicked to create a multi step form and it seems like the I am not fetching the :id properly.
Here is the profiles_controller.rb following 'signup' and creating the 1st step of profile
def create
#profile = Profile.new(profile_params[:profile])
if #profile.save
session[:profile_id] = #profile.id
redirect_to profile_steps_path
else
render :new
end
end
Here is the profile_steps_controller.rb which is the next step of form it is redirected to
class ProfileStepsController < ApplicationController
include Wicked::Wizard
steps :personal
def show
#profile = Profile.new(params[:profile])
session[:profile_id] = #profile.id
render_wizard
end
def update
#profile = Profile.new(profile_params)
#profile.attributes = (profile_params)
render_wizard #profile
end
private
def profile_params
params.require(:profile).permit(:description, :name, :website)
end
def redirect_to_finish_wizard
redirect_to root_url, notice: "Thank you for signing up."
end
end
views/profile_steps/personal.html.erb
<%= form_for #profile, url: wizard_path do |f| %>
<br>
<div class="field">
<%= f.label :name, "Company Name" %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :website %><br>
<%= f.text_field :website %>
</div>
<br>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes.rb
project::Application.routes.draw do
resources :profiles
resources :profile_steps
devise_for :users, :controllers => { :registrations => "registrations" }
root "pages#home"
get "profile" => "pages#profile"
match "profile_steps/personal", to: "profile_steps#personal", via: "post"
Thanks in advance and my apologies if this has been addressed in previous posts.
Updated:
Here is the 1st page of the multistep form:
new.html.erb
<%= form_for(#profile) do |f| %>
<% if #profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#profile.errors.count, "error") %> prohibited this profile from being saved:</h2>
<ul>
<% #profile.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<h1>New profile</h1>
<div>
<%= f.radio_button ..., true %> <%= f.label ... %>
</div>
<div>
<%= f.radio_button ..., false %> <%= f.label ... %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I had this problem in the view on the first page of the wizard, I solved it using a helper method:
def safe_previous_wizard_path(id)
id ? previous_wizard_path : root_path
end
or even:
<% if applicant.persisted? %>
Back
<% end %>

_error_messages does not work for forms

I am working on a project that a user can create a post and others can send posts about that topic.My resources file is :
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :topics , only: [ :show, :create, :destroy] do
resources :posts, only: [:create, :new]
My topics_form.html.erb:
<%= form_for(#topic) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :title, placeholder: "yeni başlık girin..." %>
</div>
<%= f.submit "Gönder", class: "btn btn-large btn-primary" %>
<% end %>
my create action is :
def create
#topic = current_user.topics.build(params[:topic])
if #topic.save
flash[:success] = "Konu oluşturuldu!"
redirect_to root_path
else
render 'static_pages/home'
end
end
My posts_form.html.erb is :
<%= form_for [#topic, #post] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "yorumunuzu girin..." %>
</div>
<%= f.submit "Gönder", class: "btn btn-large btn-primary" %>
<% end %>
my post_controller create action is :
def create
#topic= Topic.find(params[:topic_id])
#post = #topic.posts.build(params[:post])
#post.user = current_user
#post.topic_id = #topic.id
if #post.save
flash[:success] = "Yorum oluşturuldu!"
redirect_to topic_path(#topic)
else
render 'static_pages/home'
end
end
This is my error_messages.html.erb:
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
If I test the empty post and empty topic. I get this error :
undefined method `each' for nil:NilClass
Extracted source (around line #2):
1:
2: <% #topics.each do |topic|%>
3: <li><%=link_to topic.title, topic_path(topic) %></li>
4:
5: <%= will_paginate #topics %>
My static_pages_controller.rb :
def home
if signed_in?
#topic = current_user.topics.build if signed_in?
end
#topics = Topic.paginate :page => params[:page], :per_page => 20
end
and my home.html.erb:
<% if signed_in? %>
<div class="row">
<%= render 'shared/user_info' %>
<%= render 'shared/topic_form' %>
<ol class="topics-signedin">
<%= render 'shared/topics' %>
</ol>
Why does not errors does not show ?
You are missing #topics in your 'create' action. Yes, it gets set in your 'home', which rendered the form, but on submission, it passed to 'create', which then has to load the variables again, and in your case, there is no #topics set when it goes to render 'static_pages/home'. You need..
...
if #post.save
flash[:success] = "Yorum oluşturuldu!"
redirect_to topic_path(#topic)
else
#topics = Topic.paginate :page => params[:page], :per_page => 20
render 'static_pages/home'
end
You never set #topics, only #topic. So #topics is nil.
And on error you render the static_pages/home, which requires those. You should re-render the new or edit instead.

rails 3 associations error

I have a table pages and a table author. Each page belongs to one author. Created migrations for the tables and models as well. But getting this error while using it in form:
NoMethodError in Pages#new
Showing C:/rorapp/app/views/pages/_form.html.erb where line #1 raised:
undefined method `build' for nil:NilClass
Extracted source (around line #1):
1: <%= form_for(#page, #page.author.build) do |f| %>
2: <% if #page.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(#page.errors.count, "error") %> prohibited this post from being saved:</h2>
Trace of template inclusion: app/views/pages/new.html.erb
Rails.root: C:/rorapp
Application Trace | Framework Trace | Full Trace
app/views/pages/_form.html.erb:1:in `_app_views_pages__form_html_erb___998576952_54480300'
app/views/pages/new.html.erb:2:in `_app_views_pages_new_html_erb__638997451_40207104'
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
Here is my model file for author:
class Author < ActiveRecord::Base
has_one :page
end
And page model:
class Page < ActiveRecord::Base
validates :title, :presence => true
belongs_to :author
end
And here is the snippet of model form :
<%= form_for(#page, #page.author.build) do |f| %>
<% if #page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#page.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #page.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
Any ideas how to go ahead with it?
thanks
EDIT - 1
Here is my action method called new :
def new
#page = Page.new
#page.build_author
end
And here is the form it is rendering:
<%= form_for(#page) do |f| %>
<% if #page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#page.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #page.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.label :author %><br />
<%= f.text_field :author %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :reference %><br />
<%= f.select(:reference,[['google',1],['yahoo',2],['MSN',3],['Ask',4]]) %>
</p>
<%= f.submit "Submit" %>
<% end %>
EDIT - 2
Here is my controller code:
class PagesController < ApplicationController
def index
#total = Page.count
#pages = Page.find(:all)
end
def show
#page = Page.find(params[:id])
end
def new
#page = Page.new
#page.build_author
end
def create
#page = Page.new(params[:page])
if #page.save
redirect_to pages_path, :notice => "The data has been saved!"
else
render "new"
end
end
def edit
#page = Page.find(params[:id])
end
def update
#page = Page.find(params[:id])
if #page.update_attributes(params[:page])
redirect_to pages_path, :notice => "Your post has been updated!"
else
render "edit"
end
end
def destroy
#page = Page.find(params[:id])
#page.destroy
redirect_to pages_path, :notice => "Your page has been deleted!"
end
end
you need to add *#page.build_author* in the new action of pages controller.
def new
#page = Page.new
#page.build_author
respond_to do |format|
format.html # new.html.erb
format.json { render json: #page }
end
end
It should be #page.build_author instead of #page.author.build. But the logic of this still doesn't look right to me.

Resources