I'm building an app where members can create a trip and I'm using the devise gem for authorization.
When a member logs in they are redirected to the trip new page where there is a form. For some reason, I can't get the form syntax just right and I'm running into an error: undefined method `member_trips_path'
This is the trip new form
<%= form_for [current_member, #trip] do |f| %>
<%= f.label :where, "Where?" %><br/>
<%= f.text_field :where, placeholder: "Hawaii" %><br>
<%= f.label :when, "When?" %><br/>
<%= f.text_field :when, placeholder: "#" %><br>
<%= f.label :price_per_person, "Price per person? (Approximately)" %><br/>
<%= f.text_field :price_per_person, placeholder: "$550" %><br>
<%= f.submit "Create Trip Idea"%>
<% end %>
current member is equivalent to #member (this is what the devise gem does)
rake routes
member_trip_index GET /members/:member_id/trip(.:format) trip#index
POST /members/:member_id/trip(.:format) trip#create
new_member_trip GET /members/:member_id/trip/new(.:format) trip#new
edit_member_trip GET /members/:member_id/trip/:id/edit(.:format) trip#edit
member_trip GET /members/:member_id/trip/:id(.:format) trip#show
PATCH /members/:member_id/trip/:id(.:format) trip#update
PUT /members/:member_id/trip/:id(.:format) trip#update
DELETE /members/:member_id/trip/:id(.:format) trip#destroy
Trip controller
class TripController < ApplicationController
def index
end
def new
#trip = Trip.new
end
def create
end
end
Your routing is wrong - I think you have defined something like resources :trip when it should be resources :trips. Mixing singulars and plurals in routing is dangerous and leads to odd errors.
Related
I'm trying to build a form that has an input for a question, and five inputs, each of which represents an answer choice.
My problem is that in my /show.html.erb file, it throws an error which I can't figure out.
For instance:
QUESTION
ANS1
ANS2
ANS3
ANS4
ANS5
Here's what I have so far:
# app/views/mcqs/new.html.erb
<%= form_for :mcq, url: mcqs_path do |f| %>
<%= f.label :title %><br>
<%= f.text_field :q %>
<%= f.label :ans1 %><br>
<%= f.text_field :ans1 %>
<%= f.label :ans2 %><br>
<%= f.text_field :ans2 %>
<%= f.label :ans3 %><br>
<%= f.text_field :ans3 %>
<%= f.label :ans4 %><br>
<%= f.text_field :ans4 %>
<%= f.label :ans5 %><br>
<%= f.text_field :ans5 %>
<%= f.label :category_id %><br>
<%= f.number_field :category_id %>
<%= f.label :tags %><br>
<%= collection_check_boxes(:mcq, :tag_ids, Tag.all, :id, :name) %>
<%= f.submit %>
<% end %>
The controller:
# app/controllers/mcqs_controller.rb
class McqsController < ApplicationController
def new
#Mcq = Mcq.new
end
def index
#questions = Mcq.all
end
def show
#Mcq = Mcq.find(params[:id])
end
def create
#Mcq = Mcq.new(params[:mcqs])
# #Mcq.save returns a boolean indicating whether the article was saved or not.
if #Mcq.save
redirect_to #Mcq
else
render 'new'
end
end
end
The view:
# app/news/mcqs/show.html.erb
<strong>MCQ Title:</strong><br>
<%= mcq.q %>
<strong>Question Text:</strong><Br>
<%= mcq.ans1 %>
...<strong>MCQ Title:</strong><br>
<%= mcq.q %>
<strong>Question Text:</strong><Br>
<%= mcq.ans1 %>
...
The error:
NameError in Mcqs#show
Showing app/views/mcqs/show.html.erb where line #4 raised:
undefined local variable or method `mcq' for #<#
<Class:0x007fad811936e8>:0x007fad838a47a0>
<strong>MCQ Title:</strong><br>
<%= mcq.q %>
</p><p>
How can I display the input of the new.html.erb on the show.html.erb without this error?. <%= #mcq.q %> doesn't work.
In your show method you create #Mcq as Mcq.find(params[:id]), but then in your show view you want to access to it as mcq, so you need to access it the same way (name) as you declared it within the controller.
Try with:
<strong>MCQ Title:</strong><br>
<%= #Mcq.q %>
If you create a #mcq on the controller (show method), and then you want to access to it in the view which responds to that method using #Mcq, then you will receive an object with NilClass, that's to say, if the names don't match, they won't work.
Also if you use #mcq on the controller, and then you want to access as mcq, that won't work neither, the one on your controller is an instance variable, available to be used within your views coming from your controllers, the second one is a local variable, and most probably it'll raise an undefined local variable or method 'variable' error.
I can quote to #Anhubaw with:
The main difference between local and instance variable is that local
variable is only available in controller, where as instance variable
is available in corresponding views also. The controller and views do
not share local variables
Change all #Mcq in controller to #mcp, and change all mcq in view to #mcq, also change :mcq in new view to #mcq.
The variable prefix with # means it is an instance variable, it can be accessible in the view, while the normal variable doesn't start with # is just a local variable, it just can be used in the controller action method.
I would really like to add a form field "invite_code" to the user sign up form, but I don't know how to add the invite_code to the controller so that the application knows how to look for it?
The form in the sign up on the template would read:
<% form_for User.new do |f| %>
<span>Email:</span> <% f.email %><br>
<span>Name:</span> <% f.name %><br>
<span>Invite Code:</span> <% f.invite_code %><br>
<% end %>
The "invite_code" isn't part of the database or anything, but in the user registration model, I want to put a:
before_save :invite_promo
def invite_promo
if #invite_code.present? && #invite_code == "special_person"
self.special_key = true
end
end
Is there an easy way to look for form fields in the template using the model or controller?
So sorry...I'm new to Rails. Thank you so much in advance!
You need to define a virtual attribute invite_code in User model:
attr_accessor :invite_code
Your form should look as follows:
<%= form_for User.new do |f| %>
<span>Email:</span> <%= f.email_field :email %><br>
<span>Name:</span> <%= f.text_field :name %><br>
<span>Invite Code:</span> <%= f.text_field :invite_code %><br>
<% end %>
if you don't want to create a field inside your table, you can use hidden field in view like, <%= hidden_field_tag :invite_code, 'code' %>
I am very new at Ruby on Rails, and I am not quite sure what this error means:
uninitialized constant StorevaluesController
I have a storevalue_controller.rb which works (I can get to content on the page) but when I try to submit a form like this:
new.html.erb
<h1>Fill out form to add to db</h1>
<%= form_for :storevalue, url: storevalue_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
rails throws the error I reference above.
storevalue_controller.rb
class StorevalueController < ApplicationController
def new
end
def create
#storevalue = Storevalue.new(storevalue_params)
#storevalue.save
redirect_to #storevalue
end
def show
#storevalue = Storevalue.find(params[:id])
end
private
def storevalue_params
params.require(:storevalue).permit(:title, :text)
end
end
my route trace:
welcome_index_path GET /welcome/index(.:format) welcome#index
root_path GET / welcome#index
storevalue_new_path GET /storevalue/new(.:format) storevalue#new
storevalue_path POST /storevalue(.:format) storevalues#create
new_storevalue_path GET /storevalue/new(.:format) storevalues#new
edit_storevalue_path GET /storevalue/edit(.:format) storevalues#edit
GET /storevalue(.:format) storevalues#show
PATCH /storevalue(.:format) storevalues#update
PUT /storevalue(.:format) storevalues#update
DELETE /storevalue(.:format) storevalues#destroy
Note the error is plural "values" and your actual controller name is not. You're using the plural name somewhere when it doesn't exist. In Ruby, class names are constants. Hence you have the error message wording, which I agree is pretty misleading at face value.
I am new to Rails, and just working my way through my first solo project, but I seem to be running into a syntax error with a constant not being initialized (Ive gotten several of these, but each seems to have a different cause.....not quite sure how i keep getting the same error with different causes :)):
uninitialized constant DatastringsController::Datastrings
DatastringsController:
class DatastringsController < ApplicationController
def new
end
def create
#datastrings = Datastrings.new(datastrings_params) #ERROR returned on this line
#datastrings.save
redirect_to #datastrings
end
def show
#datastrings = Datastrings.find(params[:id])
end
private
def datastrings_params
params.require(:datastrings).permit(:title, :text)
end
end
I believe my form is correct:
<%= form_for :datastrings, url: datastrings_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
The main problem you have is here:
<%= form_for :datastrings, url: datastrings_path do |f| %>
form_for really should be populated with an ActiveRecord object, as this allows Rails to build the relative paths it requires correctly.
Although I don't know why this is the case, your current setup is basically trying to render DatastringsController::Datastrings -- primarily because you've not set up your form_for correctly
--
Fix
If you want to create a datastring object, I'd follow convention and do this:
#config/routes.rb
resources :datastrings
#app/controllers/datastrings_controller.rb
Class DatastringsController < ApplicationController
def new
#datastring = Datastring.new
end
def create
#datastring = Datastring.new(datastring_params)
#datastring.save
end
private
def datastring_params
params.require(:datastring).permit(:title, :text)
end
end
#app/views/datastrings/new.html.erb
<%= form_for #datastring do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
--
YOU ALSO NEED TO NAME YOUR MODELS IN SINGULAR
Looking at it now, it seems that your main issue is likely that you've named your model as a plural.
The reason this will be bad is that when you load Rails, it will load all your models, and consequently, allow you to call them by referencing their class name. If a model is plural, I don't think it will load it correctly, causing the error you've highlighted
If you name your model to the following, it should be better:
#app/models/datastring.rb
Class Datastring < ActiveRecord::Base
end
So I've been banging my head against the wall trying to figure out why this isn't working. I keep getting
ActionView::Template::Error:
undefined method `admin_information_index_path' for #<#<Class:0x007fc67971cab8>:0x007fc67d775740>
With the trace:
# ./app/views/admin/informations/_form.html.erb:1:in `_app_views_admin_informations__form_html_erb__2815737811953353352_70245242566200'
# ./app/views/admin/informations/new.html.erb:2:in `_app_views_admin_informations_new_html_erb___3700624853666844924_70245242606040'
Any tips in the right direction?
My routes:
namespace :admin do
resources :informations
end
My controller:
class Admin::InformationsController < Admin::AdminController
def new
#information = Information.new
end
end
views/admin/informations/new.html.erb:
<h1>Add New Information Page</h1>
<%= render :partial => 'form', locals: { information: #information } %>
views/admin/informations/_form.html.erb:
<%= form_for [:admin, information] do |f| %>
<%= error_messages_for information %>
<%= f.label :title %><br>
<%= f.text_field :title %><br><br>
<%= f.label :content %><br>
<%= f.text_area :content %><br><br>
<%= f.submit "Submit" %>
<% end %>
Output of rake routes
admin_informations GET /admin/informations(.:format) admin/informations#index
POST /admin/informations(.:format) admin/informations#create
new_admin_information GET /admin/informations/new(.:format) admin/informations#new
edit_admin_information GET /admin/informations/:id/edit(.:format) admin/informations#edit
admin_information GET /admin/informations/:id(.:format) admin/informations#show
PUT /admin/informations/:id(.:format) admin/informations#update
DELETE /admin/informations/:id(.:format) admin/informations#destroy
admin_root /admin(.:format) admin/sessions#new
Try just
<%= form_for information ,:namespace=>'admin' do |f| %>
UPDATE:
Look at your routes 'informations' pluralized, but your using the singular form 'information'
You must use correct form of controller, because:
'information'.pluralize
is
"information", not informations.
So, rename controller and view folder.
I'm not sure if this will work... Just a guess.
form_for #admin.information or something along those lines.