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.
Related
I have been stuck with this issue for too long now - so hoping someone might be able to tell me what I am doing wrong. I am trying to add a simple blog to my website, using rails ajax through the remote: true option - everything works fine apart from editing a post.
Everything has been generated via the scaffold command and I am under the impression that it actually should be working.
As mentioned, what does not work is the editing of a post.
Error that is returned:
ActionController::UrlGenerationError in Posts#edit
Showing ~/webapp-rails4/app/views/posts/_form.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"posts", :locale=>#<Post id: 6, title: "blog post nummer 2", body: "så prøver vi igen", created_at: "2014-11-02 11:42:19", updated_at: "2014-11-02 11:42:19", image: "Sk_rmbillede_2014-06-23_kl._21.31.58.png">, :id=>nil, :format=>nil} missing required keys: [:locale, :id]
1: <% logger.debug 'form being rendered'%>
2: <%= form_for #post, remote: true do |f| %>
3:
4:
5: <div class="field">
app/views/posts/_form.html.erb:2:in _app_views_posts__form_html_erb__3358809483585932967_70366784059520'
app/views/posts/edit.js.erb:2:in _app_views_posts_edit_js_erb___2360131588799055390_70366783957680'
my edit link:
<%= link_to 'Edit', edit_post_path(post.id, locale: I18n.locale), remote: true %>
controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
respond_to :js, :html
def edit
logger.debug 'controller action edit'
respond_with(#post)
end
def create
#post = Post.new(post_params)
#post.save
#respond_with(#post)
end
def update
#post.update(post_params)
#respond_with(#post)
end
private
def set_post
#post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :body, :image)
end
edit.js.erb (I have tried multiple things here, this is what I have at the moment):
alert('this is edit.js.erb');
$('#post<%= #post.id %> #editPostModal').append("<%= escape_javascript(render 'form', object: #post) %>");
$('#post<%= #post.id %> #editPostModal').foundation('reveal', 'open');
_form.html.erb(seems to be what is throwing the error):
<% logger.debug 'form being rendered'%>
<%= form_for #post, remote: true, :multipart => true, authenticity_token: true 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 %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div>
<%= f.label :remote_image_url, "or image URL" %><br>
<%= f.text_field :remote_image_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
rake routes:
posts GET /:locale/posts(.:format) posts#index {:locale=>/en|dk/}
POST /:locale/posts(.:format) posts#create {:locale=>/en|dk/}
new_post GET /:locale/posts/new(.:format) posts#new {:locale=>/en|dk/}
edit_post GET /:locale/posts/:id/edit(.:format) posts#edit {:locale=>/en|dk/}
post GET /:locale/posts/:id(.:format) posts#show {:locale=>/en|dk/}
PATCH /:locale/posts/:id(.:format) posts#update {:locale=>/en|dk/}
PUT /:locale/posts/:id(.:format) posts#update {:locale=>/en|dk/}
DELETE /:locale/posts/:id(.:format) posts#destroy {:locale=>/en|dk/}
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
I am currently going through "Agile web development with Rails - 4th edition" and I am stuck.
The checkout button is supposed to bring up an orders page, but instead I get this error:
NoMethodError in Orders#new
Showing C:/Sites/depot/app/views/orders/_form.html.erb where line #1 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for(#order) do |f| %>
2: <% if #order.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(#order.errors.count, "error") %>
After spending a lot of time trying to figure this out, I copied all the source code from the books website, which still didn't work. I even checked the errata and book forums and found nothing.
What am I doing wrong?
This is the code from the views/orders/_form.html.erb:
<%= form_for(#order) do |f| %>
<% if #order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#order.errors.count, "error") %>
prohibited this order from being saved:</h2>
<ul>
<% #order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<!-- START_HIGHLIGHT -->
<%= f.text_field :name, size: 40 %>
<!-- END_HIGHLIGHT -->
</div>
<div class="field">
<%= f.label :address %><br />
<!-- START_HIGHLIGHT -->
<%= f.text_area :address, rows: 3, cols: 40 %>
<!-- END_HIGHLIGHT -->
</div>
<div class="field">
<%= f.label :email %><br />
<!-- START_HIGHLIGHT -->
<%= f.email_field :email, size: 40 %>
<!-- END_HIGHLIGHT -->
</div>
<div class="field">
<%= f.label :pay_type %><br />
<!-- START_HIGHLIGHT -->
<%= f.select :pay_type, Order::PAYMENT_TYPES,
prompt: 'Select a payment method' %>
<!-- END_HIGHLIGHT -->
</div>
<div class="actions">
<!-- START_HIGHLIGHT -->
<%= f.submit 'Place Order' %>
<!-- END_HIGHLIGHT -->
</div>
<% end %>
This is the Orders Controller:
class OrdersController < ApplicationController
def index
#orders = Order.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #orders }
end
end
# GET /orders/1
# GET /orders/1.json
def show
#order = Order.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #order }
end
end
# GET /orders/new
# GET /orders/new.json
def new
#cart = current_cart
if #cart.line_items.empty?
redirect_to store_url, notice: "Your cart is empty"
return
end
#order = Order.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #order }
end
end
# GET /orders/1/edit
def edit
#order = Order.find(params[:id])
end
# POST /orders
# POST /orders.json
def create
#order = Order.new(params[:order])
#order.add_line_items_from_cart(current_cart)
respond_to do |format|
if #order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
format.html { redirect_to store_url, notice:
'Thank you for your order.' }
format.json { render json: #order, status: :created,
location: #order }
else
#cart = current_cart
format.html { render action: "new" }
format.json { render json: #order.errors,
status: :unprocessable_entity }
end
end
end
# PUT /orders/1
# PUT /orders/1.json
def update
#order = Order.find(params[:id])
respond_to do |format|
if #order.update_attributes(params[:order])
format.html { redirect_to #order, notice: 'Order was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
#order = Order.find(params[:id])
#order.destroy
respond_to do |format|
format.html { redirect_to orders_url }
format.json { head :ok }
end
end
end
This is the Order model:
class Order < ActiveRecord::Base
PAYMENT_TYPES = [ "Check", "Credit card", "Purchase order" ]
has_many :line_items, dependent: :destroy
# ...
validates :name, :address, :email, presence: true
validates :pay_type, inclusion: PAYMENT_TYPES
end
The form view believes that #order is nil here:
1: <%= form_for(#order) do |f| %>
What is the path that you are hitting in the browser? /orders/new
It does appear that Order#new is setting #order to Order.new, so I am wondering if you are actually hitting that controller.
Rails will route requests to controller actions based on what is setup in routes.rb. Run rake routes to see the routes you can access in the url.
Rails has conventions such that if you access the /orders/new using a simple GET request, it will route your request to the new action of the OrdersController and will attempt to render the associated app/views/orders/new.html.erb - unless you customize some aspect of this.
I would add a puts "Order is set to: #{#order}" in the new action following the #order = Order.new assignment to ensure it's actually going through that path.
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?
Rails 3.1.3
I am making a simple site where people can share short stories and can rate those stories on a 5 Star rating system. The Star rating system is the problem. I can get it to work fine in the stories/show.html view, but not on the indexed home page. Here is my code:
home.html.erb
<% content_for(:scripts) do %>
<%= javascript_include_tag 'rating_ballot'%>
<% end %>
<div id="talesFeedHome">
<p class="notice"><%= notice %></p>
<%= render #tales.sort_by { |tale| tale.created_at }.reverse %>
</div>
<p class="clear"> </p>
tales/_tale.html.erb
<% if signed_in? %>
<div id="homeTales">
<ul>
<div id="taleShow">
<div id="controlPanel">
<li id="taleUserName"><%= tale.user.name %></li>
<li id="averageRating"> Your Rating:<br /><%= render "tales/stars" %></li>
</div>
<div id="taleDisplay">
<li><%= link_to(tale) do %>
<span><%= tale.title %> </span>
<span><%= tale.content %></span>
<% end %>
</li> <br />
</div>
</div>
</ul>
</div>
<% else %>
...
tales/_stars.html.erb
<div id="starRating">
<%= form_for(rating_ballot, :remote => true, :html => { :class => 'rating_ballot' }) do |f| %>
<%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
<%= radio_button_tag("rating[value]", 1, current_user_rating == 1, :class => 'rating_button') %>
<%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
<%= radio_button_tag("rating[value]", 2, current_user_rating == 2, :class => 'rating_button') %>
<%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
<%= radio_button_tag("rating[value]", 3, current_user_rating == 3, :class => 'rating_button') %>
<%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
<%= radio_button_tag("rating[value]", 4, current_user_rating == 4, :class => 'rating_button') %>
<%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
<%= radio_button_tag("rating[value]", 5, current_user_rating == 5, :class => 'rating_button') %>
<%= hidden_field_tag(:tale_id, #tale.id) %>
<% end %>
</div>
It is at this point, in the hidden field tag, that I get this error:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Here are the 3 relevant controllers:
pages_controller.rb
class PagesController < ApplicationController
def home
#tales = Tale.all
end
end
tales_controller.rb
class TalesController < ApplicationController
respond_to :html, :js
def new
#tale = Tale.new
end
def show
#tale = Tale.find(params[:id])
end
def index
#tales = Tale.all
#tale = Tale.find(params[:id])
end
...
ratings_controller.rb
class RatingsController < ApplicationController
before_filter :authenticate_user!
respond_to :html, :js
def create
#tale = Tale.find_by_id(params[:tale_id])
#rating = Rating.new(params[:rating])
#rating.tale_id = #tale.id
#rating.user_id = current_user.id
if #rating.save
respond_to do |format|
format.html { redirect_to #tale, :notice => "Your rating has been saved" }
format.js
end
end
end
def update
#rating = current_user.ratings.find_by_tale_id(params[:tale_id])
#tale = #rating.tale
if #tale and #rating.update_attributes(params[:rating])
respond_to do |format|
format.html { redirect_to #tale, :notice => "Your rating has been updated" }
format.js
end
end
end
end
The problem is here somewhere. Somehow when rendering #tales on the home page, this invalidates the #tale.id on the _stars partial. I can not figure out how to solve this. Thank You.
ok, I think your value of #tales is nil so please do the following things and let me know what would be the result
first put raise #tales.Inspect in your home method in your PagesController
second <%= raise tale.inspect%> in _tale.html.erb.
want to check weather value of tale is null or not.