NoMethodError (undefined method `projects' for nil:NilClass): - ruby-on-rails

NoMethodError (undefined method projects' for nil:NilClass):
app/controllers/project_controller.rb:8:inindex'
Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb
(21.7ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.2ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
(3.6ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb
within rescues/layout (89.7ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb
(0.6ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb
within layouts/inlined_string (0.6ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb
within layouts/inlined_string (0.7ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb
within layouts/inlined_string (0.5ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (51.7ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb
within layouts/javascript (1.2ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.9ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (116.8ms)
Here is a project controller:
class ProjectController < ApplicationController
before_action :confirm_logged_in
before_action :find_company
def index
#projects = #company.projects.sorted
end
def show
#project = Project.find(params[:id])
end
def new
#project = Project.new()
#project_count = Project.count + 1
#companys = Company.order("position ASC")
end
def create
#project = Project.new(project_params)
if #project.save
redirect_to(:action => 'index')
else
#project_count = Project.count + 1
#companys = Company.order("position ASC")
render('new')
end
end
def edit
#project = Project.find(params[:id])
#project_count = Project.count
#companys = Company.order("position ASC")
end
def update
#project = Project.find(params[:id])
if #project.update_attributes(project_params)
redirect_to(:action => 'index')
else
#project_count = Project.count
#companys = Company.order("position ASC")
render('new')
end
end
def delete
#project = Project.find(params[:id])
end
def destory
#project = Project.find(params[:id])
#project.destroy
redirect_to(:action => 'index')
end
private
def project_params
params.require(:project).permit(:name, :position, :type_of_project, :description, :no_of_tasks)
end
def find_company
if params[:company_id]
#company = Company.find(params[:company_id])
end
end
end

The error means #company is nil. The company is from the method find_company, you can either correct your find_company logic, or provide a company_id parameter in your request. You can also adjust the index method to get all projects if #company is nil like so:
def index
#projects = Project.all.sorted
end
It all depends on how you want to load the projects.

What is your link url or button url to go the index page. You need to pass :company_id params to make it work.
e.g.
projects_path(:company_id => Company.first.id)

Related

Posts Rating - Rails

I am implementing rating system in rails for posts.
On viewing a post one can rate the post by clicking radio button.
Below is the code. Consider only the post and rating don't consider tags, topics..
And there is no user in my concept one can rate whenever he needed and it should be added with existing rating of the post.
But, When I am doing this in log it shows the following:
Server log:
Started PATCH "/posts/34" for 127.0.0.1 at 2015-12-08 18:36:55 +0530
Processing by PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"l3aae99V424OyKVt5ULmqX2Mcs7DY2GYBskbLyhqqNENDn24ldCDAt4gNcgjESlFR6eaP0vcvrcoOerGE9lH5A==", "post"=>{"rating_ids"=>["5"]}, "commit"=>"Rate", "id"=>"34"}
Post Load (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 34]]
CACHE (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "34"]]
(0.0ms) begin transaction
SQL (4.0ms) INSERT INTO "ratings" ("star", "post_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["star", 5], ["post_id", 34], ["created_at", "2015-12-08 13:06:55.626133"], ["updated_at", "2015-12-08 13:06:55.626133"]]
(216.0ms) commit transaction
(0.0ms) begin transaction
Rating Load (1.0ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."id" = ? LIMIT 1 [["id", 5]]
Rating Load (0.0ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."post_id" = ? [["post_id", 34]]
SQL (2.0ms) UPDATE "ratings" SET "post_id" = NULL WHERE "ratings"."post_id" = ? AND "ratings"."id" IN (4, 25) [["post_id", 34]]
SQL (3.0ms) UPDATE "ratings" SET "post_id" = ?, "updated_at" = ? WHERE "ratings"."id" = ? [["post_id", 34], ["updated_at", "2015-12-08 13:06:55.878147"], ["id", 5]]
(170.0ms) commit transaction
Redirected to http://localhost:3000/topics/9
Completed 302 Found in 489ms (ActiveRecord: 397.0ms)
where it changes the post.id to NULL
SQL (2.0ms) UPDATE "ratings" SET "post_id" = NULL WHERE "ratings"."post_id" = ? AND "ratings"."id" IN (4, 25) [["post_id", 34]]
I don't know how this happens and how to overcome this So, Please help.
It changes the Rating database as below:
1st column: id, 2nd column: star, 3rd column: post_id
1,1,NULL
2,2,NULL
3,3,NULL
4,4,NULL
5,5,34
6,4,NULL
7,1,NULL
8,1,NULL
9,5,NULL
10,1,NULL
11,5,NULL
12,1,NULL
13,4,NULL
14,3,NULL
15,4,NULL
16,4,NULL
17,4,NULL
18,2,NULL
19,1,NULL
20,5,NULL
21,3,NULL
Post model:
class Post < ActiveRecord::Base
belongs_to :topic
has_many :comments
has_and_belongs_to_many :tags
has_many :ratings
end
Rating model:
class Rating < ActiveRecord::Base
belongs_to :post
end
Post show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #posts.name %> (
<%= #posts.topic.name %> )
</p>
<p>
<strong>Email:</strong>
<%= #posts.email %>
</p>
<p>
<strong>Message:</strong>
<%= #posts.message %>
</p>
<strong>Tags:</strong>
<% #posts.tags.each do |tag| %>
<div>
<%= tag.name %> <br>
</div>
<% end %>
<br>
<strong>Rating:</strong>
<%= #posts.ratings.group(:star).count %>
<%= form_for #posts do |f| %>
<% (1..5).each do |rating| %>
<%= radio_button_tag "post[rating_ids][]", rating %>
<%= rating %>
<% end %>
<%= f.submit('Rate') %>
<% end %>
<%= link_to 'Comments', post_comments_path(#posts) %>
<%= link_to 'Edit', edit_post_path(#posts) %> |
<%= link_to 'Back', topic_posts_url(#posts.topic) %>
Post controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
if params[:topic_id].to_i > 0
#topic = Topic.find(params[:topic_id])
#posts = #topic.posts.paginate(page: params[:page], per_page: 10)
else
#posts = Post.eager_load(:topic).paginate(page: params[:page], per_page: 10)
end
end
# GET /posts/1
# GET /posts/1.json
def show
#posts = Post.find(params[:id])
#tags = #posts.tags
#comment = Comment.new(:post => #posts)
end
# GET /posts/new
def new
#topic = Topic.find(params[:topic_id])
#posts = #topic.posts.new
end
# GET /posts/1/edit
def edit
#posts = Post.find(params[:id])
#tags = #posts.tags
end
# POST /posts
# POST /posts.json
def create
#topic = Topic.find(params[:topic_id])
#posts = #topic.posts.build(post_params)
respond_to do |format|
if #posts.save
format.html { redirect_to topic_url(#posts.topic_id), notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #posts }
else
format.html { render :new }
format.json { render json: #posts.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
#posts = Post.find(params[:id])
#tags = #posts.tags
respond_to do |format|
#posts.ratings.create(:star => params[:post][:rating_ids][0].to_i)
if #posts.update(post_params)
format.html { redirect_to topic_url(#posts.topic_id), notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #posts }
else
format.html { render :edit }
format.json { render json: #posts.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#posts.destroy
respond_to do |format|
format.html { redirect_to topic_url(#posts.topic_id), 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
#posts = 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(:name, :email, :message, :topic_id, {tag_ids:[]}, rating_ids:[])
end
end
I am new to rails and I need to implement this without using any gem..
Please help how it changes the post_id to NULL which I denoted in server log..
This is because after you created a Rating manually then goes #posts.update(rating_ids:[5]), which states that this post only should have rating with id 5, not 5 stars
Plus are you sure want your unauthenticated users to have access to post editing?
Since you save the rating before updating the post change your post_params to:
def post_params
params.require(:post).permit(:name, :email, :message, :topic_id, {tag_ids:[]})
end
As you are saving the rating from the params directly.
However you should consider changing your structure to use something like nested attributes which will automatically save/update ratings.

Ruby on Rails - Add Comments to Products

I am creating my first application in Rails, an e-commerce and would like to associate comments to the products, I created all the necessary structure, I have the user model, the products and the comments, I think I did everything right but when I go to render the form to insert the comment I get this error:
First argument in form can not contain nil or be empty
else
object = record.is_a?(Array) ? record.last : record
raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
object_name = options[:as] || model_name_from_record_or_class(object).param_key
apply_form_for_options!(record, object, options)
end
the view that renders the form is this
<%= form_for(#comment, html: {multipart: true}) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new comment..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<span class="picture">
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<% end %>
<script type="text/javascript">
$('#comment_picture').bind('change', function() {
size_in_megabytes = this.files[0].size/1024/1024;
if (size_in_megabytes > 8) {
alert('Maximum file size is 8MB. Please choose a smaller file.');
}
});
Comments Controller
class CommentsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy, :show]
before_action :correct_user, only: :destroy
def show
#comment = Comment.find(params[:id])
end
def create
#comment = current_user.comment.build(comment_params)
if #comment.save
flash[:success] = "Comment created!"
redirect_to store_url
else
#feed_items = []
render 'static_pages/home'
end
end
def destroy
#comment.destroy
flash[:success] = "Comment deleted"
redirect_to request.referrer || store_url
end
private
def comment_params
params.require(:comment).permit(:content, :picture)
end
def correct_user
#comment = current_user.comments.find_by(id: params[:id])
redirect_to store_url if #comment.nil?
end
end
Show of Product Controller
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
#products = Product.paginate(page: params[:page])
end
# GET /products/1
# GET /products/1.json
def show
#product = Product.find(params[:id])
#comments = current_user.comments.build if logged_in?
#comments = #product.comments.paginate(page: params[:page])
#rating = Rating.where(product_id: #product.id, user_id: #current_user.id).first
unless #rating
#rating = Rating.create(product_id: #product.id, user_id: #current_user.id, score: 0)
end
end
User Controller
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
#users = User.where(activated: true).paginate(page: params[:page])
end
def show
#user = User.find(params[:id])
#comments = #user.comments.paginate(page: params[:page])
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
#user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to store_url
else
render 'new'
end
end
def update
if #user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to #user
else
render 'edit'
end
end
def edit
#user = User.find(params[:id])
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Confirms the correct user.
def correct_user
#user = User.find(params[:id])
redirect_to(store_url) unless current_user?(#user)
end
# Confirms an admin user.
def admin_user
redirect_to(store_url) unless current_user.admin?
end
end
I can post you also believe the models if necessary, thanks to all who help me.
Log file:
Started GET "/en/products/4" for 127.0.0.1 at 2015-07-12 20:30:01 +0200
Processing by ProductsController#show as HTML
Parameters: {"locale"=>"en", "id"=>"4"}
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", 4]]
CACHE (0.0ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", "4"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rating Load (0.2ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."product_id" = ? AND "ratings"."user_id" = ? ORDER BY "ratings"."id" ASC LIMIT 1 [["product_id", 4], ["user_id", 1]]
(0.2ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."product_id" = ? [["product_id", 4]]
(0.2ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."user_id" = ? [["user_id", 1]]
Rendered shared/_user_info.html.erb (3.0ms)
Rendered shared/_error_messages.html.erb (0.1ms)
(0.2ms) SELECT SUM("ratings"."score") FROM "ratings" WHERE "ratings"."product_id" = ? [["product_id", 4]]
CACHE (0.0ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."product_id" = ? [["product_id", 4]]
Rendered shared/_comment_form.html.erb (7.6ms)
CACHE (0.0ms) SELECT SUM("ratings"."score") FROM "ratings" WHERE "ratings"."product_id" = ? [["product_id", 4]]
CACHE (0.0ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."product_id" = ? [["product_id", 4]]
Rendered products/show.html.erb within layouts/application (24.3ms)
Rendered layouts/_header.html.erb (5.9ms)
Rendered layouts/_footer.html.erb (1.1ms)
Completed 200 OK in 205ms (Views: 197.7ms | ActiveRecord: 1.1ms)
Started POST "/comments?locale=en" for 127.0.0.1 at 2015-07-12 20:30:11 +0200
Processing by StaticPagesController#home as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"1sIh3jNERiiitmbyHQaQpAomMIgjXKkBVA/RqsIYDhYkD73gFOeMKyYYtmjSr9mu0ngyKSephYmfJhvvlKMigg==", "comment"=>{"content"=>"Hello! "}, "commit"=>"Post", "locale"=>"comments"}
comments translation not available
Rendered static_pages/home.html.erb within layouts/application (70.2ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered layouts/_header.html.erb (4.4ms)
Rendered layouts/_footer.html.erb (0.6ms)
Completed 200 OK in 249ms (Views: 246.7ms | ActiveRecord: 0.2ms)

ActionController::ParameterMissing (param is missing or the value is empty: bid)

When I try to customize my bid_params to add a parameter to the database, my strong parameters are not working for some reason. I need to be able to have the current_user passed into the database on the creation of a bid. This object is nested in a has_many belongs_to relationship with Auctions. Here is my controller:
class BidsController < ApplicationController
def index
#auction = Auction.find(params[:auction_id])
#bids = #auction.bids
end
def new
#auction = Auction.find(params[:auction_id])
#bid = #auction.bids.build
end
def create
#auction = Auction.find(params[:auction_id])
#bid = #auction.bids.create(bid_params)
if #bid.save
flash[:success] = "Bid has been successfully placed."
redirect_to #auction
else
flash[:error] = #bid.errors.full_messages.join('. ')
render 'new'
end
end
def destroy
#auction = Auction.find(params[:auction_id])
#bid = #auction.bids.find
#bid.destroy
flash[:notice] = "Successfully destroyed Bid."
redirect_to auction_url(#bid.article_id)
end
private
def bid_params
params.require(:bid).permit(:auction_id).merge(bidder: current_user)
end
end
and Stack Trace:
Started POST "/auctions/2/bids" for 127.0.0.1 at 2014-12-06 08:54:35 -0600
Processing by BidsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6x4hV8y323a10kaJN5Rubj1z3uhUrSDQrD6aoaWCUhk=", "commit"=>"Create Bid", "auction_id"=>"2"}
Auction Load (0.1ms) SELECT "auctions".* FROM "auctions" WHERE "auctions"."id" = ? LIMIT 1 [["id", 2]]
Completed 400 Bad Request in 2ms
ActionController::ParameterMissing (param is missing or the value is empty: bid)
New Form:
<h1>Create a New Bid</h1>
<%= form_for ([#auction, #bid]) do |f|%>
<p>
<%= f.submit %>
</p>
<%end%>
Thanks!
Look at parameters that controller received:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6x4hV8y323a10kaJN5Rubj1z3uhUrSDQrD6aoaWCUhk=", "commit"=>"Create Bid", "auction_id"=>"2"}
And then you try to permit these params:
def bid_params
params.require(:bid).permit(:auction_id).merge(bidder: current_user)
end
And error was thrown in this operation: params.require(:bid) as this method suppose your params looks like:
{ ..., "bid" => { "auction_id" => "2" } }
Hence you may change your view/js that sends params or change def bid_params implementation to:
def bid_params
params.permit(:auction_id).merge(bidder: current_user)
end

ActionController::UrlGenerationError in Comments#show

I use polymorphic associations with comments. When I try to add 'edit' and also 'destroy' in show template, I get the error in the title (just edit for now). How do I add both links to show?
comments_controller.rb
class CommentsController < ApplicationController
....
before_action :signed_in_user, only: [:new, :edit]
before_filter :load_commentable
def index
#commentable = load_commentable
#comments = #commentable.comments
end
def show
end
def edit
#commentable = load_commentable
end
def new
#commentable = load_commentable
#comment = #commentable.comments.new
end
def create
#comment = #commentable.comments.new(comment_params)
#comment.user = current_user
if #comment.save
redirect_to #comment, notice: "Created."
else
render :new
end
end
def update
#comment = #commentable.comments.build(comment_params)
#comment.user = current_user
respond_to do |format|
if #comment.update(comment_params)
format.html { redirect_to #comment, notice: 'It was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
private
def load_commentable
resource, id = request.path.split('/')[1, 2]
#commentable = resource.singularize.classify.constantize.find(id)
end
....
end
show.html.erb template
<%= link_to "Edit", [:edit, #commentable, :comment] %>
form
<%= form_for [#commentable, #comment] do |f| %>
....
full trace
log
Processing by CommentsController#show as HTML
Parameters: {"post_id"=>"1", "id"=>"2"}
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE
"comments"."id" = ? LIMIT 1 [["id", "2"]]
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" =
? LIMIT 1 [["id", "1"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" =
? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
(0.2ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."user_id" =
? [["user_id", 2]] CACHE (0.0ms) SELECT COUNT(*) FROM "comments"
WHERE "comments"."user_id" = ? [["user_id", 2]]
Rendered comments/show.html.erb within layouts/application (10.7ms)
Completed 500 Internal Server Error in 19ms
ActionView::Template::Error (No route matches {:action=>"edit",
:controller=>"comments", :post_id=>#, :id=>nil,
:format=>nil} missing required keys: [:id]):
25: <div class="thumbsdown"><%= link_to image_tag('othericons/thumbiconDown.PNG', height: '20', width: '20'),
"#" %>
26: <%= link_to image_tag('othericons/flagicon.PNG', height: '20', width: '18'), "#"
%>
27:
28: <%= link_to "Edit", [:edit, #commentable, :comment] %> 29:
30:
31: app/views/comments/show.html.erb:28:in
`_app_views_comments_show_html_erb___2937579164590753686_69833853514120'
Rendered /home/action/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(1.6ms) Rendered
/home/action/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb
(1.2ms) Rendered
/home/action/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb
within rescues/layout (19.1ms)
routes:
resources :posts do
resources :comments
end
Change the Edit Link as below:
<%= link_to "Edit", edit_post_comment_path(#commentable, #comment) %>
You have setup Post and Comment as nested routes, so you need to pass objects of Post as well as Comment to the edit path.
EDIT
For Polymorphic association, you could use it as below:
<%= link_to "Edit", [:edit, #commentable, #comment] %>

can't register my variable in the database

i get an error when i clic on my button sumbit
the error say undefined method `strftime' for nil:NilClass
when i debugge my application i see something strange
tarted POST "/users/2/calendars" for 127.0.0.1 at 2013-05-24 19:44:25 +0200
Processing by CalendarsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"8l0+IDaC+tAHdQi+yvpnWEJRHrR8hNwBA3zgaSBoovQ=", "calendar"=>{"event"=>"aze", "published_on"=>"2013-05-07", "description"=>"rrrrrrr"}, "commit"=>"Create Calendar", "user_id"=>"2"}
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO `calendars` (`created_at`, `description`, `event`, `published_on`, `updated_at`, `user_id`) VALUES ('2013-05-24 17:44:26', NULL, NULL, NULL, '2013-05-24 17:44:26', 2)
(79.8ms) COMMIT
Redirected to http://127.0.0.1:3000/users/2/calendars/39
Completed 302 Found in 93ms (ActiveRecord: 80.3ms)
cache: [GET /users/2/calendars/39] miss
why he doesn't take my variable event, published_on description i dont understand .
i have missing something ?
calendar.model
class Calendar < ActiveRecord::Base
attr_accessible :event, :published_on, :description, :user_id
belongs_to :user#, :foreign_key => :user_id
end
user.model
has_many :calendar#, :foreign_key => :user_id
accepts_nested_attributes_for :calendar
#attr_accessible :calendar_id
end
calendar.controler
def index
#user = current_user
##content_calendars = #user.Calendar
#content_calendars = #user.Calendar.all
#content_calendars_by_dates = #content_calendars.group_by(&:published_on)
#date = params[:date] ? Date.parse(params[:date]) : Date.today
end
def show
#user = current_user
# #calendar = #user.find_all_calendar_by()
# #tasks = #project.tasks.find_all_by_complete(false)
#calendar = #user.calendar.find(params[:id])
end
def new
#user = current_user
#calendar = #user.calendar.new
##calendar = #user.calendar.build
end
# GET /calendars/1/edit
def edit
#user = current_user
#calendar = #user.Calendar.find(params[:id])
end
# POST /calendars
# POST /calendars.json
def create
#user = current_user
#calendar = #user.calendar.new(params[:current_user])
##calendar = #user.Calendar.new(params[:calendar])
if #calendar.save
#redirect_to calendar_path(#user)
redirect_to [#user,#calendar]
#, notice: "L evenement a ete cree"
else
render :new
end
end
# PUT /calendars/1
# PUT /calendars/1.json
def update
#user = current_user
#calendar = #user.Calendar.find(params[:id])
if #calendar.update_attributes(params[:id])
redirect_to [#user, #calendar]#, notice: "L article a ete mis a jour"
else
render :edit
end
end
# DELETE /calendars/1
# DELETE /calendars/1.json
def destroy
#user = current_user
#calendar.destroy
respond_to do |format|
format.html { redirect_to calendars_url }
format.json { head :no_content }
end
end
private
def find_user
#user = current_user
##user = User.first
##user ||= User.find(session[:user_id]) if session[:user_id]
#findession[:user_id])
end
thanks again for your help guy
I don't really understand where you are facing error, so please paste the code of Controller. Based on your explanation, I am feeling that you should get calender's parameter like below. I think your controller should create object like this from calender parameters:
#calender = Calender.new(params[:calender])
#calender.user_id = params[:user_id]
Hope that helps!!!

Resources