Ruby On Rails: Active Recode method Save is undefined when called in Link Controller, - activerecord

I', working on a Ruby On Rails App, and I am getting the following error when I attempt to call the Save method from Active Record to populate a new link to the database. This is code that goes wrong, its in the Create method in my LinksController class:
def create
#link = Link.new(params[:link])
respond_to do |format|
if #product.save
format.html { render :action => "create" }
format.json { render :json => #link }
else
format.html { render :action => "new" }
format.json { render :json => #product.errors, :status => :unprocessable_entity }
end
end
end
When I go to
http://localhost:3000/links/new
And attempt to create a new link with this form:
<%= form_for(#link) do |f| %>
<% if #link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#link.errors.count, "error") %> prohibited this link from being saved:</h2>
<ul>
<% #link.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :url %><br />
<%= f.text_field :url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
and click submit, I get the following error:
undefined method `save' for nil:NilClass
I have no idea what is going on, so if anyone has an answer, or even pointers, I would really appreciate it. Thanks.

#link = Link.new(params[:link])
respond_to do |format|
if #product.save
so where did #productcome from?

Related

rails application displaying blank space in users(venue) place

I am able to post an event but when I select the user to post as, the dropdown menu shows empty spaces where the user (venue name) should be. I have tried everything that I can think of. Any ideas would be greatly appreciated.
I am on rails 4. I am using devise as well.
here are my files:
show.html.erb:
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #gig.user.venue_name %>
</p>
<p>
<strong>Event:</strong>
<%= #gig.event %>
</p>
<p>
<strong>Date:</strong>
<%= #gig.date %>
</p>
<p>
<strong>Doors:</strong>
<%= #gig.doors %>
</p>
<p>
<strong>Showtime:</strong>
<%= #gig.showtime %>
</p>
<p>
<strong>Age:</strong>
<%= #gig.age %>
</p>
<p>
<strong>Price:</strong>
<%= #gig.price %>
</p>
<p>
<strong>Description:</strong>
<%= #gig.description %>
</p>
<%= link_to 'Edit', edit_gig_path(#gig) %> |
<%= link_to 'Back', gigs_path %>
index.html.erb:
<p id="notice"><%= notice %></p>
<div class="page-header">
<h1>All Gigs</h1>
</div>
<% #gigs.each do |gig| %>
<div class="gig">
<strong><%= gig.user.venue_name %></strong>
<p><%= gig.event %></p>
<p><%= gig.date %></p>
<p><%= gig.doors %></p>
<p><%= gig.showtime %></p>
<p><%= gig.age %></p>
<p><%= gig.price %></p>
<p><%= gig.description %></p>
<div class="meta">
<%= link_to time_ago_in_words(gig.created_at) + " ago", gig %>
<span class="admin">
| <%= link_to 'Edit', edit_gig_path(gig) %> |
<%= link_to 'Delete', gig, method: :delete, data: { confirm: 'Are you sure?' } %>
</span>
</div>
</div>
<% end %>
</tbody>
</table>
gig.rb:
class Gig < ActiveRecord::Base
belongs_to :user
end
user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :gigs
end
_form.html.erb:
<%= simple_form_for(#gig, html: {class: "form-horizontal"}) do |f| %>
<% if #gig.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#gig.errors.count, "error") %> prohibited this gig from being saved:</h2>
<ul>
<% #gig.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id, collection: User.all, label_method: :venue_name %>
<%= f.input :event %>
<%= f.input :date %>
<%= f.input :doors %>
<%= f.input :showtime %>
<%= f.input :age %>
<%= f.input :price %>
<%= f.input :description %>
<div class="form-actions">
<%= f.button :submit %>
</div>
db migrate add user id to gig
class AddUserIdToGigs < ActiveRecord::Migration
def change
add_column :gigs, :user_id, :integer
add_index :gigs, :user_id
remove_column :gigs, :name
end
end
gigs_controller.rb
class GigsController < ApplicationController
before_action :set_gig, only: [:show, :edit, :update, :destroy]
# GET /gigs
# GET /gigs.json
def index
#gigs = Gig.all
end
# GET /gigs/1
# GET /gigs/1.json
def show
end
# GET /gigs/new
def new
#gig = Gig.new
end
# GET /gigs/1/edit
def edit
end
# POST /gigs
# POST /gigs.json
def create
#gig = Gig.new(gig_params)
respond_to do |format|
if #gig.save
format.html { redirect_to #gig, notice: 'Gig was successfully created.' }
format.json { render :show, status: :created, location: #gig }
else
format.html { render :new }
format.json { render json: #gig.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /gigs/1
# PATCH/PUT /gigs/1.json
def update
respond_to do |format|
if #gig.update(gig_params)
format.html { redirect_to #gig, notice: 'Gig was successfully updated.' }
format.json { render :show, status: :ok, location: #gig }
else
format.html { render :edit }
format.json { render json: #gig.errors, status: :unprocessable_entity }
end
end
end
# DELETE /gigs/1
# DELETE /gigs/1.json
def destroy
#gig.destroy
respond_to do |format|
format.html { redirect_to gigs_url, notice: 'Gig was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gig
#gig = Gig.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def gig_params
params.require(:gig).permit(:name, :event, :date, :doors, :showtime, :age, :price, :description, :user_id)
end
end
Solved it:
After running irb, I noticed that venues was null in the db. Therefore, I realized that I had to as venues in devise by creating new forms.

undefined local variable or method `comment' for #<#<Class:0xb6d2fd0>:0xc077dd8>

undefined local variable or method `comment' for #<#<Class:0xb6d2fd0>:0xc077dd8>
This is what my _comment.html.erb partial looks like
<div class="media">
<%= link_to '#', class: 'pull-left' do %>
<%= image_tag(comment.user.avatar.small.url) if comment.user.avatar? %>
<% end %>
<div class="media-body">
<small>
<%= comment.user.name %> commented <%= time_ago_in_words(comment.created_at) %> ago
<% if policy(comment).destroy? %>
| <%= link_to "Delete", [#topic, #post, comment], method: :delete %>
<% end %>
</small>
</div>
The create section of my comments_controller.rb
def create
#post = Post.find(params[:post_id])
#comment = current_user.comments.build(comment_params)
#comment.post = #post
authorize #comment
if #comment.save
redirect_to #post, notice: "Comment was saved successfully."
else
flash[:error] = "Error creating comment. Please try again."
render :new
end
end
and how I rendered in my posts#show view
<div>
<%= render :partial => "comments/comment", local: {comment: #comment} %>
</div>
You're not passing any locals to this partial:
<%= render :partial => "comments/comment" %>
You'd want to do this:
<%= render :partial => "comments/comment", :locals => { :comment => #comment } %>
Or use the shortcut:
<%= render 'comments/comment', :comment => #comment %>
You're declaring a local variable named comment in your _comment.html.erb partial.
You'll need to pass the #comment from your controller like this:
<%= render :partial => "comments/comment", locals: { comment: #comment } %>
Take a look at Layouts and Rendering Rails Guide for more info.

Ruby on Rails First argument in form cannot contain nil or be empty

I used the standard Ruby on Rails generate scaffolding. I need help to find out how to move a form from the "New" view to the "Posts" view (Main Page). I keep on getting the error "First argument in form cannot contain nil or be empty". Here is my "Posts" view code. It works fine in the "New" view but not the "Posts" view.
Posts View:
<%= form_for(#post) do |f| %>
New View:
<h1>New post</h1>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>
Form View:
<%= 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 |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
<div class="field">
<%= f.label :casino %>
<%= f.text_field :casino %>
</div>
</div>
<div class="field">
<%= f.label :City %>
<%= f.text_field :City %>
</div>
</div>
</div>
<div class="field">
<%= f.label :State %>
<%= f.text_field :State %>
</div>
</div>
<div class="field">
<%= f.label :Country %>
<%= f.text_field :Country %>
</div>
<div class="field">
<%= f.label :Game %>
<%= f.text_field :title %>
<div class="field">
<%= f.label :Minimum %>
<%= f.text_field :text %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
CONTROLLER CODE:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :text, :casino, :City, :State, :Country)
end
end
Add this line in index action of PostsController
#post = Post.new
This should solve your problem :)
SOLUTION:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
#post = Post.new
end

Like button does not update with AJAX

I am passing a collection (#feed_items) to a _feed_item partial via the :collection option and converting it to dailypost with :as => :dailypost.
Inside the _feed_item partial I rendered another partial for _like_button, and i used :locals to continue using dailypost.
Everything works fine with the database. Likes get added and taken out :) BUT
I am trying to use (AJAX) to create.js.erb & destroy.js.erb the like button.
For some reason only the top post gets updated correctly and i have to refresh the page to see the ones below it.
I know the solution involves assigning a unique post id to each "like" element, say "like-123", but that is where i am stuck.......
I also know that the problem may be in _feed_items.html.erb because i am passing two ids....any suggestions??
Views
_feed.html.erb
<% if #feed_items.any? %>
<ol>
<%= render partial: 'shared/feed_item', collection: #feed_items, :as => :dailypost %>
</ol>
<% end %>
_feed_items.html.erb
<li id="<%= #dailypost.id %>">
<span class="user">
<%= link_to dailypost.user.name, dailypost.user %>
<span class="content"><%= dailypost.content_html %></span>
<div id="like">
<%= render :partial => 'likes/like_button', :locals =>{:dailypost => dailypost} %>
</div>
</li>
_like_button.html.erb
<% if like = current_user.likes.find_by_dailypost_id(dailypost.id) %>
<%= form_for like, :html => { :method => :delete }, :remote => true do |f| %>
<%= f.submit "Unlike" %>
<% end %>
<% else %>
<%= form_for current_user.likes.build, :remote => true do |f| %>
<div><%= f.hidden_field :dailypost_id, value: dailypost.id %></div>
<%= f.hidden_field :user_id %>
<%= f.submit "Like" %>
<% end %>
<% end %>
create.js.erb
$("#like").html("<%= escape_javascript(render :partial => 'like_button', :locals => {:dailypost => #dailypost}) %>");
destroy.js.erb
$("#like").html("<%= escape_javascript(render :partial => 'like_button', :locals => {:dailypost => #dailypost}) %>");
Controller
class LikesController < ApplicationController
respond_to :html, :js
def create
#like = Like.create(params[:like])
#dailypost = #like.dailypost
respond_to do |format|
format.js
format.html { redirect_to :back }
end
end
def destroy
like = Like.find(params[:id]).destroy
#dailypost = like.dailypost
respond_to do |format|
format.js
format.html { redirect_to :back }
end
end
end
Hmmm.Replace the first-line in _feed_items.html.erb with this.
<li id="dailypost<%= dailypost.id %>">
and In create.js.erb and destroy.js.erb, make changes
$("#like").html(...)
To
$("#dailypost<%= dailypost.id%> #like").html(...)
This should Work.

Form validation after ajax request

I'm using Rails 3.2 and I'm making my first Ajax request.
This is my code:
def create
#post = Post.find(params[:post_id])
#comment = Comment.new(params[:comment])
respond_to do |format|
if #comment.save
format.html {
redirect_to #post
}
format.js
else
format.html {
render 'posts/show'
}
format.js
end
end
end
So, I understand that I return a js template.
So, question 1: Is really a good idea to use this approach instead the standard sending json / receiving json in a $.ajax call?
My create.js.erb:
$(".commentlist").append("<%= escape_javascript(render(:partial => #comment)) %>");
Working perfectly.
But... how I validate the form? (Without ajax works perfectly).
I'm seeing that the js.erb is like the html.erb but with js instead of html (I still see this weird, I think that is easier to use controller.coffee, but I will learn this way too :)).
So question 2: How I validate ? I see that what I get in the ajax call is the entire html and my two action vars (#post and #comment).
Just that, I need to learn the Rails approach to do ajax :)
P.S: My _form.html.erb:
<%= form_for comment, :url => post_comments_path(comment.post), :remote => true do |f| %>
<% if comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(comment.errors.count, "error") %> prohibited this comment from being added:</h2>
<ul>
<% comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :post_id %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :url %>
<%= f.text_field :url %>
</p>
<p>
<%= f.label :content %>
<%= f.text_area :content %>
</p>
<p class="no-border">
<%= f.submit "Post", :class => "button" %>
</p>
<% end %>

Resources