uninitialized constant with rails friendships - ruby-on-rails

I'm new to rails and getting the following error:
NameError in FriendshipsController#create
uninitialized constant FriendshipsController
this also shows up:
{"authenticity_token"=>"eQvv3flATE+P1TEErOWP/6fM8dEOIBxltobCxtM/F18=",
"friend_id"=>"32"}
When I click on the "Add Friend" Link on my users show page. I am following the railscast about self referential associations to a T, but I keep getting this error and I can't find any information about it, not even what "uninitialized constant" means. I've gathered from the internet that it MAY be related to the acts_as_authenticated plugin, but I followed the one fix I found and it didn't work.
Here is the code from my user/show.html.erb page:
<%= link_to "Add Friend", friendships_path(:friend_id => #user.id), :method => :post %>
and the code from my friendships controller:
def create
#friendship = current_user.friendships.build(:friend_id => params[:friend_id])
if #friendship.save
flash[:notice] = "Added friend."
redirect_to root_url
else
flash[:error] = "Unable to add friend."
redirect_to root_url
end
end
Where am I going wrong here. I haven't the faintest clue what is causing this. Please let me know if I am missing any needed code.

Difficult to tell. You should post the top part of your class... requires, class definition, includes, and anything else you have that is outside of your methods, as well as the create method.

Rails is complaining because you have used a constant before initializing it.
puts SomeConstant
# before
SomeConstant = 10
In this case the constant is a controller Class Name - FriendshipsController
Check if the class name is correct, i.e. you have a controller with that name in your app\controller directory.

I think you run
rails g controller Friendship
while you should have used
rails g controller Friendships
thats why all files are now singular
You can still go through and change all files though

Related

getting Id from created record in rails

New web developer here, and I think I may be missing some very fundamental knowledge. Given the code
> def create
> #post = Post.new(post_params)
> if #post.save
> redirect_to #post
> else
> render "new"
> end
end
after saving the post, it redirects to show page, due to this "redirect_to #post", how can I do the same thing with "redirect_to: action => "show", :id => 5" I have to pass the ID now, how to retrieve the ID from #post object?
so only I can pass the Id to redirect page.
can I stop the compiler here, like debugger in js?
To answer your question of "I may be missing some very fundamental knowledge" yes, you might be. An object in Rails like #post is usually a database record. You can access any of it's columns in the DB by using the column name as a method:
#post.id
returns:
5 #or whatever the post id is.
If your post table has a column of "title" you can access it with
#post.title
returns:
"This is an awesome post"
I would highly recommend you view some Ruby and some Rails tutorials. Everything in Ruby is an object. Rails uses a lot of conventions so you can do things without having to write code for it, it's already there for you. When you get into Rails ActiveRecord Relations you'll see that relations expand this to give you related table information as methods. For Example:
Post.rb
...
belongs_to :user
User.rb
...
has_many :posts
Gives you methods like:
#post.user #returns the user object with all of its info
#post.user.username #returns the value of that column for that user
#post.user.posts #returns an array of Post objects that belong to the owner of that post.
Ruby has a pry-byebug gem for debugging. It's a combination REPL (Pry) and core debugger (byebug) that work very powerfully together.
Getting the id of a successfully saved ActiveRecord model is just #post.id, however the rails methods like redirect_to will take the object itself just fine, as #Beartech has mentioned, above. The documentation shows a variety of ways to use it, for convenience:
redirect_to action: "show", id: 5
redirect_to #post
redirect_to "http://www.rubyonrails.org"
redirect_to "/images/screenshot.jpg"
redirect_to posts_url
redirect_to proc { edit_post_url(#post) }

Rails 4: 'redirect_to current_thing' undefined error

I have a model “Thing,” each of which has_many “Comments,” each of which in turn has_many “Votes.” I want to be able to vote on comments on the Thing show page. This is what I have so far:
Comments Controller:
def votecomment
#comment = Comment.find(params[:id])
Vote.create!(voteable_id: params[:id], voteable_type: 'Comment')
redirect_to current_thing
end
Things View:
<%= link_to “Vote”, vote_comment_path(:id => comment.id), method: :post %>
Routes:
post 'comments/:id/vote' => 'comments#vote', as: 'vote_comment'
But I'm getting back this error:
NameError in CommentsController#votecomment
undefined local variable or method `current_thing' for #<CommentsController:0x007f98efa69c00>
I tried moving the method to the Things controller, but I got the exact same type of error.
What am I doing wrong?
Assuming you have the following relation in comment.rb
belongs_to :thing
You can access the thing object of a comment using #comment.thing. Since redirect_to accepts objects, you can do
redirect_to #comment.thing
You have to understand that nothing is called current_thing if you are familiar with devise and you see ex current_user this is a method in the gem not a populated method with each model you create.
So if you want something like that add method to your application_controller or even application helper to get current_thing
def current_thing
Thing.find() --> or whatever the way you get that current thing.
end

Rails Tutorial 10.2 - 6: "NoMethodError in Users#edit"

I've been walking through the Hartl tutorial for the first time and have been getting an error on Chapter 10 editing/updating users. Following along, I >>should<< be able to at least view the user edit page, but keep getting the below error. Have gone through everything a few times but can't seem to find the origin of my problem. Note that this is my first time doing anything programming related so go slow with me.
Error
undefined method `model_name' for NilClass:Class
Line being called out in error, from my user edit view
<%= form_for(#user) do |f| %>
Edit method from Users controller
def edit
#user = User.find(params[:id])
#title = "Edit user"
end
A similar block of code is used in my user new view with no issues, and am at a loss why this would be returning nil and have tried with both new and existing users . Thanks!
I just had the same issue, it looks like this happens because your "edit" action isn't defined. Could it be you have either not saved the user_controller file or forgotten to define the "edit" variable?
The error is a bit obscure. It is saying that #user in <%= form_for(#user) do |f| %> is nil. It is likely that #user = User.find(params[:id]) is not finding anything. It would be worth putting Rails.logger.debug("Id: #{params[:id]}") above #user = User.find(params[:id]) and see what is being passed for params[:id].
This SO post had a similar issue though not with the tutorial. The exception is coming from form_for.

How to destroy polymorphic model? Method destroy missing argument

Im using this gem to add private messages to my application.
https://github.com/LTe/acts-as-messageable/blob/master/lib/acts-as-messageable/message.rb
I`m trying to add remove link to message.
So in my controller i have destroy action:
def destroy
#message = current_user.messages.with_id(params[:id])
if #message.destroy
flash[:notice] = "All ok"
else
flash[:error] = "Fail"
end
end
And in my view i have link: = link_to "Delete", message_path(message.id), :method => :delete
But when im trying to click link i receive: wrong number of arguments (0 for 1)
This is related with this question: Why delete method gives me wrong path? with
The problem is that you're getting all messages, so #message is really multiple messages. You probably want to do:
#message = Message.find(params[:id])
But this may be different for the gem. The gem's documentation has a section on deleting at the bottom of the readme.

undefined method `activated?'

I'm trying to implement Authlogic in Rails 3 and have just been having headache after headache...I'm extremely new to rails, so please forgive me for not being an expert. I followed the railscast on the subject which has been really helpful, but as soon as i submit my create new user form via the actual website I get this:
undefined method `activated?'
app/controllers/users_controller.rb:37:in `create'
Any help would be SO appreciated...had a headache with this tonight...
Code from create method:
def create
#user = User.new(params[:user])
if #user.save
flash[:notice] = "Registration successful."
else
render :action => 'new'
end
end
If anyone else hits this issue - regenerate your user_session model and fill it with:
class UserSession < Authlogic::Session::Base
def to_key
new_record? ? nil : [ self.send(self.class.primary_key) ]
end
end
This fixed it for me...seems to be an error surrounding that model at the very least so take it back to basics!
The problem for me was the existence of a user_sessions table. If you created the UserSession model through a generator, you have a migration that creates that table.
Simply deleting the table (both in test and development databases) and the migration file solved the problem for me.
Cheers,
-- José

Resources