I am new to RoR and trying simple CRUD operations without scafolding. I have a model called ccword, with controller named WordsController.
Problem is I am creating simple 'create' and leading me to this error:
NameError in C cwordsController#create
uninitialized constant CCwordsController
This is how my create method in my controller looks like:
def create
#ccword = CCword.new(params[:ccword])
respond_to do |format|
if #ccword.save
flash[:notice] = 'Word was successfully added.'
format.html { redirect_to(#ccword) }
format.xml { render :xml => #ccword, :status => :created, :location => #ccword }
else
flash[:notice] = 'Error.'
format.html { render :action => "show" }
format.xml { render :xml => #ccword.errors, :status => :unprocessable_entity }
end
end
end
end
my view new.html.erb looks like :
<h1>Words new</h1>
<% form_for (#ccword) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :word %><br />
<%= f.text_field :word %>
</p>
<p>
<%= f.label :meaning %><br />
<%= f.text_field :meaning %>
</p>
<p>
<%= f.submit 'create' %>
</p>
<% end %>
When I click create I get above NameError.
In console following log gets displayed:
Processing ApplicationController#create (for 127.0.0.1 at 2009-11-19 09:33:06) [POST]
Parameters: {
"commit"=>"Create",
"authenticity_token"=>"hqf54HkXSaTPGejGSbEPcw3ZpOy1Ayp3gvti29gc7Eg=",
"c_cword"=>{"meaning"=>"2", "word"=>"2"}
}
NameError (uninitialized constant CCwordsController):
Any idea?
The problem is here:
format.html { redirect_to(#ccword) }
try changing it to
format.html { redirect_to word_path(#ccword) }
Related
I'm writing a CMS and I faced a problem while making a role management for users. I have a boolean field :admin in my User model, and I've made a checkbox in my form to set created user as an administrator. Here is the users_controller:
def create
#user = User.create(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to users_path }
format.json { head :no_content }
else
format.html { render :new }
format.json { render #user.errors, status: :unprocessable_entity }
end
end
end
def edit
end
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to users_path }
format.json { head :no_content }
else
format.html { render :edit }
format.json { render #user.errors, status: :unprocessable_entity }
end
end
end
and this is my form :
<%= form_for #user do |f| %>
# Here go fields for username, email and password
<p>
<%= f.label "Set as administrator" %> <br />
<%= f.hidden_field :admin, '' %>
# I also tried with <%= f.hidden_field :admin, false %>
<%= f.check_box :admin, checked = true %>
# Or <%= f.check_box :admin, data: { switch: true } %>
</p>
<% end %>
But any of these options returns me the following:
NoMethodError in Multiflora::Users#edit
undefined method `merge' for "":String
What have I done wrong?
Take hidden field as
<%= f.hidden_field :admin, value: '' %>, or simply
<%= f.hidden_field :admin %>
and checkbox as <%= f.check_box :admin, :checked => true %>
<%= form_for #user do |f| %>
# Here go fields for username, email and password
<p>
<%= f.label "Set as administrator" %> <br />
<%= f.hidden_field :admin, value: '' %>/<%= f.hidden_field :admin %>
<%= f.check_box :admin, :checked => true %>
</p>
<% end %>
Okay so my associations are:
Outlet -> has_many :monitorings
Monitoring -> belongs_to :outlet
My Routes:
resources :outlets do
resources :monitorings
end
View:
<%= link_to new_outlet_monitoring_path(#outlet) %>
When I click the link, the logs show that the outlet_id is passed as a parameter to the new page correctly.
But when saving the monitoring record, the outlet_id becomes nil.
Any help?
UPDATE:
# views/monitorings/_form.html.erb
<%= form_for(#monitoring) do |f| %>
<h2>Type of Monitoring</h2>
<fieldset data-role="controlgroup" >
<div class="radio-group">
<%= f.radio_button :mtype, "Full" %><%= f.label :mtype, "Full", value: "Full" %>
<%= f.radio_button :mtype, "Partial" %><%= f.label :mtype, "Partial", value: "Partial" %>
<%= f.radio_button :mtype, "None" %><%= f.label :mtype, "None", value: "None" %>
</div>
</fieldset>
<hr>
<%= f.submit "Next Step" %>
<% end %>
And the controller:
# controllers/monitoring_controller.rb
def new
#monitoring = Monitoring.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #monitoring }
end
end
def create
#monitoring = Monitoring.new(params[:monitoring])
respond_to do |format|
if #monitoring.save
format.html { redirect_to #monitoring, notice: 'Monitoring was successfully created.' }
format.json { render json: #monitoring, status: :created, location: #monitoring }
else
format.html { render action: "new" }
format.json { render json: #monitoring.errors, status: :unprocessable_entity }
end
end
end
This is most likely an issue with the way you are creating the new monitoring record. Can we see your form and your create controller action?
On form submission, it's telling me that it was successfully created but it's not showing any data that was submitted. The database is empty. It's showing "null" values and the same on the actual screen where I should be able to edit the data. Here's a screenshot
Update: I think the problem is that it's making a GET request but I don't know how to fix it. Here's a screen shot of my server doing a get when I clicked the submit
Here's the set up
In the index action of results_controller.rb, I have
def index
#results = Result.all
#blob = Sex.new //==#blob = Sex.new is the one I'm focussing on...
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #results }
end
end
In views/results/index, I have the form
<`%= form_for(#blob) do |f| %>`
<div class="field">
<b>1. solicitor exam was fixed?:</b><br/>
<%= f.label(:solicitorcurve, "it was cooked") %>
<%= f.radio_button(:solicitorcurve, "t") %> </br>
</div>
<div class="field">
<%= f.label(:solicitorcurve, "no it was ok") %>
<%= f.radio_button(:solicitorcurve, "f") %>
</div>
<div class="field">
<%= f.label(:draftingteach, "i give the teaching a grade of _ on a scale of 1 to 6") %>
<%= f.select:draftingteach, 1..6 %> </br>
</div>
In the create action of sexes_controller.rb i have
def create
#sex = Sex.new(params[:blob])
respond_to do |format|
if #sex.save
format.html { redirect_to(#sex, :notice => 'Sex was successfully created.') }
format.xml { render :xml => #sex, :status => :created, :location => #sex }
else
format.html { render :action => "new" }
format.xml { render :xml => #sex.errors, :status => :unprocessable_entity }
end
end
end
In models/sex.rb, there is nothing...
class Sex < ActiveRecord::Base
end
And this is the set up of the database
It looks like the issue is that you're retrieving params[:blob] when you should be looking at params[:sex]. form_for will create fields named after the class of the object. The instance variable name #blob you're using is arbitrary.
...
#sex = Sex.new(params[:sex])
...
This is a good argument for why you probably want to name instance variables for what they are. Less confusion.
Couldn't find Submission without an ID
This problem has haunted me for last few days, and I can't seem to fix it.
This problem occurs when I try to create an Emailinterest object in the app.
I have two models.
Submission
Emailinterest
For every submission, there can be several emailinterest, but emailinterest cannot exist
C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
submission_id actually a member of emailinterest object. submission_id is suppose to contain the ID value of submission object.
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
#submission = Submission.find(params[:submission_id])
respond_to do |format|
if #emailinterest.save
Notifier.emailinterest_notification(#emailinterest, #submission).deliver
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
C:\Rails\actuirl5\app\views\submissions\show.html.erb
....
<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new(:submission_id=>#submission.id)} %>
C:\Rails\actuirl5\app\views\submissions
<%= form_for(emailinterest) do |f| %>
<%= hidden_field :submission_id, :value => #submission.id %>
<div class="field">
<%= f.label :sender_email %><br />
<%= f.text_field :sender_email %>
</div>
<div class="field">
<%= f.label :sender_email_content %><br />
<%= f.text_area :sender_email_content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
FIX
C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
#submission = Submission.find(params[:submission_id])
respond_to do |format|
if #emailinterest.save
Notifier.emailinterest_notification(#emailinterest, #submission).deliver
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
C:\Rails\actuirl5\app\views\submissions_form_new_emailinterest.html.erb
<%= form_for(emailinterest) do |f| %>
<%= hidden_field_tag :submission_id, value = #submission.id %>
<div class="field">
<%= f.label :sender_email %><br />
<%= f.text_field :sender_email %>
</div>
<div class="field">
<%= f.label :sender_email_content %><br />
<%= f.text_area :sender_email_content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
C:\Rails\actuirl5\app\views\submissions\show.html.erb
<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new} %>
Considering you have set #submission in your show action this should work:
<%= hidden_field :submission_id, :value => emailinterest.submission_id %>
and then
#submission = Submission.find(params[:emailinterest][:submission_id])
You cannot access instance variables like #submission in a partials. This is why you have the option :locals.
My app is something like Kijiji's email reply system.
For each post, user can choose to reply to the post.
I get this error when I submit.
Undefined method `contact_email' for nil:NilClass
I denoted the line that is causing the error below with **
emailinterests_controller.rb
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
respond_to do |format|
if #emailinterest.save
**Notifier.emailinterest_notification(self, #submission).deliver**
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
"self" refers to the emailinterest object that's being emailed.
#submission should refer to the current object that emailinterest object is interacting with.
notifier.rb
**def emailinterest_notification(emailinterest, submission)**
#emailinterest = emailinterest
#submission = submission
**mail :to => submission.contact_email,**
:from => emailinterest.sender_email,
:subject => 'actuirl.com - RE:' + submission.title
end
FIX
C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
#submission = Submission.find(params[:submission_id])
respond_to do |format|
if #emailinterest.save
Notifier.emailinterest_notification(#emailinterest, #submission).deliver
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
C:\Rails\actuirl5\app\views\submissions_form_new_emailinterest.html.erb
<%= form_for(emailinterest) do |f| %>
<%= hidden_field_tag :submission_id, value = #submission.id %>
<div class="field">
<%= f.label :sender_email %><br />
<%= f.text_field :sender_email %>
</div>
<div class="field">
<%= f.label :sender_email_content %><br />
<%= f.text_area :sender_email_content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
C:\Rails\actuirl5\app\views\submissions\show.html.erb
<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new} %>
I didn't see you define #submission at anywhere. so, It is null that is correct error. Check where #submission has been create to solve :-)
Good luck
I think the problem is that you reference to
**Notifier.emailinterest_notification(self, #submission).deliver**
instead of
**Notifier.emailinterest_notification(#emailinterest, #submission).deliver**
"self" would be the controller but I think you want the #emailinterest.