undefined local variable or method when using custom scaffolded route - ruby-on-rails

I am learning Ruby on Rails and was following a tutorial and scaffolded a CRUD app. I am trying to put link to the add a new entry in the navbar but it's giving the below error:
undefined local variable or method `phone_lists_new_path'
Here's the navbar code:
<%= link_to "List", phone_lists_path, class:"nav-item"%>
<%= link_to "Add contact", phone_lists_new_path, class:"nav-item"%>
phone_lists is a folder in View and new.html.erb is a file along with the below ones:
I guess I am doing something wrong in phone_lists_new_path, but it works in the other links like phone_lists_path and home_about_path

It was new_phone_list and not phone_lists_new, rake routes would show the route name.

Related

Rails does not navigate to controller method

I am trying to add a new controller method. I also created a corresponding view for the same. All I am trying to do is create a variable called #x and printing it in the view. I am doing this just to verify if my application is going into the method. However, I do not see the value getting printed on the view. Please help.
This is my controller named submitted_content_controller.rb
class SubmittedContentController < ApplicationController
def begin_planning
#x = 1
end
end
This is my view called begin_planning.html.erb
<%= form_tag url_for(:action => :revision_planning), method: "post" %>
<%= text_field 'questionnaire', 'name', class: "form-control width-250" %>
<p><%= #x %></p>
<button>Create</button>
This is the routes I added:
get :begin_planning
When I am in the begin_planning html page, I see the text field and create button but I don't see the value of #x getting printed. It will be a huge help. Thank you.
This is my routes output:
begin_planning_submitted_content_index GET /submitted_content/begin_planning(.:format) submitted_content_controller#begin_planning
there are other routes to which I didn't add, because this controller has many other methods which I did not add since it's a huge file.
You may be rendering the view without going through the action.
If Rails finds a view file with a name that matches a route, but doesn't find a corresponding action for that route, it will still
render the view.
https://guides.rubyonrails.org/layouts_and_rendering.html#rendering-by-default-convention-over-configuration-in-action
So, you successfully render the form in the view, but Rails
doesn't know what #x means.
In your routes file, try adding the following outside of any namespaces:
get '/begin_planning', to: 'submitted_content#begin_planning'
Your rake routes should include the following:
begin_planning GET /begin_planning(.:format) submitted_content#begin_planning
And navigate to http://localhost:3000/begin_planning
Or,
if you'd like to namespace, you can add the following to your routes file instead:
namespace :submitted_content do
get :begin_planning
end
And your 'rake routes' should include the following:
submitted_content_begin_planning GET
/submitted_content/begin_planning(.:format)
submitted_content#begin_planning
And navigate to http://localhost:3000/submitted_content/begin_planning

Rails: Access model from different controller

I know this question been ask before and they said 'models are independent of the controller.' So, following my same code I did for other models I just renamed my working code to fit to my new model. But I'm getting an error undefined method 'userimages_path' Here the code I'm using.
The model is userimage and the controller is uploads.
Controller/uploads_controller.rb
def new
#userimage = Userimage.new
end
Model/userimage.rb is an empty file
Views/uploads/new.html.erb (This line is throwing the error.)
<%= form_for #userimage do |f|%>
In my routes.rb
resources :uploads
I have rake db:migrate several times to make sure I did migrate the database thinking this might be why it can't find the Userimage.
What I have I done wrong/missing here?
It is a Rails magic, when you don't specify second option(url) for form_for, Rails try to set it, in your case <%= form_for #userimage do |f|%> converts by Rails to <%= form_for #userimage, url: userimages_path do |f|%>, in your routes, there is no such _path helper.
To resolve this issue run bundle rake routes and set the right url option.
Check the documentation
try below code:
view
<%= form_for #userimage, url: "/uploads" do |f|%>
uploads controller
def create
...
end

Rails: link_to only if route exists

I'm writing a Rails Engine and in one of my views I'd like to create a link to main_app if a named route exists.
I will give an example to clarify the question:
The Main application may or may not define in routes.rb:
resources :my_resource, only: :index
In my rails engine I need to add a link to my_resource_path only if main_app defined that route. I think it should be something like the following:
<%= link_to "Link", main_app.my_path if my_resource_path_exists? %>
I tried:
<%= link_to "Link", main_app.my_resource_path if
main_app.respond_to?(:my_resource_path) %>
but, when the path does not exist, that raises NoMethodError:
undefined method `my_resource_path' for #<Module>
How my_resource_path_exists? method could be implemented? I'm using ruby 2.2 and rails 4.2
The question is related to: Determine if path exists as route in Rails controller
But in this case, I have a named route (my_resource_path), so I need something slightly different, but can't figure out by myself.
I found a solution wich does not require to recover from fail. It's possible to check if the route is defined using url_helpers:
Rails.application.routes.url_helpers.method_defined?(:my_path)
So, the code could be written like that:
<%= link_to "Link", main_app.my_resource_path if
Rails.application.routes.url_helpers.method_defined?(:my_resource_path) %>
You could either use main_app.try(:my_path) or rescue NoMethodError to return false.

Rails undefined method in Controller

I have a Link resource (Link as in url's). I have a method in my Links controller to determine whether or not a link that a user enters has the "http://" prefix, and if not, to append that prefix to the URL. Although I defined the method in my Links controller, I am getting an undefined method error.
Here is the relevant portion of my links controller:
helper_method :link_formatter
def link_formatter(url)
prefix = "http://"
url.include?(prefix)? url : prefix + url
end
Here is my links view:
<%= link_to link.description, link_formatter(link.url), :target => '_blank' %>
The error:
undefined method `link_formatter' for #<#<Class:0x007fbd51e2ea08>:0x007fbd5250cfa0>
The link_formatter is a generic function that can be used by any view in your Rails application. To make it accessible by all your views add it to the ApplicationHelper as #Damien Roche said. The application helper is located in app/helpers/application_helper.rb. You can also generate a new helper by running rails generate helper HELPER_NAME on the command line.

Rails 4 + Devise + Routing Error

I'm attempting to install Devise on a rather simple event creating/displaying Rails 4 app. I have 2 static pages and the index page displaying without authentication, and all is well there. Anything else kicks you to the "sign up" page. From there, when I attempt to create an account for myself (to simply see the other pages- simple vanilla devise installation attempt) I get a "No route matches [POST] "/registrations/user" error when clicking "submit" (f.submit)
I am using Rails 4, have the 3.0.3 version of Devise, bundled it, ran "rails generate devise:install" to install it, ran "rails generate devise user", ran db:migrate, raked the routes, and restarted the Rails server.
I even recreated the button action with the "correct" route and "get post" - no dice.
Anybody have any idea? I'm at a loss here.
Well it's always tricking debugging sever-side code without seeing it in action, but here's what should work.
yourappname/config/routes.rb :
devise_scope :user do
# matches the url '/register' to the registration controller's 'new' action and creates a :register symbol inorder to reference the link dynamically in the rest of the code
get 'register', to: 'devise/registrations#new', as: :register
end
(Assuming your register button is in your app's partial)
yourappname/app/views/layouts/application.html.erb:
<%= link_to "Register", register_path, class: "btn" %>
Now you could actually put that link_to anywhere, in any page, and it'll work. The reason for this is the 'register_path'.
When you create your route, the as: :register parameter creates a global variable that can be accessed throughout your app.
the register_path variable in your link_to method will always call the right action, even if you change your routes file.
It's just syntax. You could create any symbol you want. This would also work:
routes.rb:
# Inside get method, third parameter
as: :bowtiesarecool
someview.html.erb:
# Inside link_to method, second parameter
bowtiesarecool_path
Just match the variable name with the symbol name and a put a _path after it. The link_to method will handle everything else!

Resources