Not saving, no error message - ruby-on-rails

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.

Related

rails passing params to view after redirect

I want to pass 2 strings to the view after redirecting.
the controller:
def create
#rating = Rating.new(params[:rating])
respond_to do |format|
if #rating.save
format.html { redirect_to #rating, :notice => 'Got It!' ,
:notice_small => 'Your photo has been uploaded. good luck with it\'s coolness rating!' }
format.json { render :json => #rating, :status => :created, :location => #rating }
else
format.html { render :action => "new" }
format.json { render :json => #rating.errors, :status => :unprocessable_entity }
end
end
end
the view:
<p id="notice" class="big_notice"><%= notice %></p>
<% if defined? notice_small %>
<p id="small_notice" class="small_notice"><%= notice_small %></p>
<% end %>
the notice string goes throw but the notice_small does not, why?
Only :notice and :alert are allowed to be set using redirect_to.
If you want something beyond this, use :flash => { :notice_small => '....' } option for redirect_to or set flash[:notice_small] before redirect_to explicitly.
The redirect looks like it should work as far as I can tell. However, to make it available in your view, the action you're redirecting to would have to take params["notice_small"] and put it in an instance variable. Something like
#notice_small = params["notice_small"]
in the actions, then you could do
<% if defined? #notice_small %>
<p id="small_notice" class="small_notice"><%= #notice_small %></p>
<% end %>

undefined method `first_name'

Can anyone shed light on this for me?
undefined method `first_name' for #
Here is the show.html
<p id="notice"><%= notice %></p>
<div id="container">
<p>
<b>First name:</b>
<%= #artist.firstname %>
</p>
<p>
<b>Second name:</b>
<%= #artist.surname %>
</p>
<p>
<b>About:</b>
<%= #artist.about %>
</p>
<div id="comments">
<h2>Comments</h2>
<%= render :partial => "shared/comment", :collection => #artist.comments%>
</div
</div>
<%= render :partial => "image", :collection => #artist.images %>
<%= link_to 'Edit', edit_artist_path(#artist) %> |
<%= link_to 'Back', artists_path %>
<%= link_to 'show', images_path %>
Here is the partial
<div class="comment">
<p>
<span class="commentator"><%= comment.commentator.display_name %>
say's</span>
<%= comment.comment %>
</p>
</div
Here is the friend view
class Friends < ActiveRecord::Base
attr_accessible :firstname, :surname
has_many :comments, :as => :commentator, :class_name =>"Commentable"
def display_name
"#{self.firstname} #{self.surname}"
end
end
This is the friends controller
class FriendsController < ApplicationController
# GET /friends
# GET /friends.xml
def index
#friends = Friend.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #friends }
end
end
# GET /friends/1
# GET /friends/1.xml
def show
#friend = Friend.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #friend }
end
end
# GET /friends/new
# GET /friends/new.xml
def new
#friend = Friend.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #friend }
end
end
# GET /friends/1/edit
def edit
#friend = Friend.find(params[:id])
end
# POST /friends
# POST /friends.xml
def create
#friend = Friend.new(params[:friend])
respond_to do |format|
if #friend.save
format.html { redirect_to(#friend, :notice => 'Friend was successfully created.') }
format.xml { render :xml => #friend, :status => :created, :location => #friend }
else
format.html { render :action => "new" }
format.xml { render :xml => #friend.errors, :status => :unprocessable_entity }
end
end
end
# PUT /friends/1
# PUT /friends/1.xml
def update
#friend = Friend.find(params[:id])
respond_to do |format|
if #friend.update_attributes(params[:friend])
format.html { redirect_to(#friend, :notice => 'Friend was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #friend.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /friends/1
# DELETE /friends/1.xml
def destroy
#friend = Friend.find(params[:id])
#friend.destroy
respond_to do |format|
format.html { redirect_to(friends_url) }
format.xml { head :ok }
end
end
end
I am trying to make it so a friend can leave a comment on an artists page but I keep getting the above error.
I am very new to Ruby so I apologise if I have left anything out.
Basically, rails will look at the database to figure out what fields are on a model. So make sure your migrations have been run, and that first_name exists on the db table.
Also, Friends is plural. In rails, your table is plural (friends), your model is singular (Friend), and your controller is plural (FriendsController). It is best not to go against this convention. Try renaming the model and see what happens
This error related to database that first_name don't exist in your db.Run migration carefully.
You need line 2 of the Friend class to be attr_accessible :firstname, :surname so your views have access to the those variables.

Rails 3 - Does ID of an object have to be an integer value?

C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
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
I keep getting stuck at this line
#submission = Submission.find(params[:submission_id])
with this error.
Couldn't find Submission without an ID
Submission and Emailinterest are both objects.
Emailinterest need some information about the members of the Submission object to be created.
submission_id actually a member of Emailinterest object. submission_id is suppose to contain the ID value of Submission object.
Now I have submission_id as string value. Does it have to be an integer value?
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} %>
This error:
Couldn't find Submission without an ID
is probably caused by
#submission = Submission.find(nil)
and nil is what a Hash will give when you ask it for a key it doesn't have (unless someone has supplied a different default of course but params and its contained Hashes should give you nil). So you don't have a :submission_id inside params[:emailinterest]. Based on your comments, I would guess that you do have params[:submission_id] so try this:
#submission = Submission.find(params[:submission_id])
and if that doesn't work, do a logger.debug params.inspect and look at your logs to see where :submission_id is (if anywhere at all).
It can be a string or anything, as long as it can be converted to an integer.
You can verify this using your rails console:
ruby-1.9.2-p0 > Product.find("1")
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", "1"]]
=> #<Product id: 1, collection_id: nil, name: "test", description: nil, price: 1000, picture_file_name: nil, picture_content_type: nil, picture_file_size: nil, picture_updated_at: nil, created_at: "2011-07-14 10:10:45", updated_at: "2011-07-14 10:10:45">
If you want to know what's really in submission_id, you can log the value by putting this at the beginning of your method:
logger.debug params[:emailinterest][:submission_id]

Rails 3 - Syntax error - Accessing member of object

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.

rails: "unknown action" message when action is clearly specified

I had hard time to figure out why I've been getting "unknown action" error message when I was do some editing:
Unknown action
No action responded to 11. Actions: bin, create, destroy, edit, index, new, observe_new, show, tag, update, and vote
you can see that Rails did mention each action in the above list - update. And in my form, I did specify action = "update".
I wonder if some friends could kindly help me with the missing links...
here is the code:
edit.rhtml
<h1>Editing tip</h1>
<% form_tag :action => 'update', :id => #tip do %>
<%= render :partial => 'form' %>
<p>
<%= submit_tag_or_cancel 'Save Changes' %>
</p>
<% end %>
_form.rhtml
<%= error_messages_for :tip %>
<p><label>Title<br/>
<%= text_field :tip, :title %></label></p>
<p><label>Categories<br/>
<%= select_tag('categories[]', options_for_select(Category.find(:all).collect {|c| [c.name, c.id] }, #tip.category_ids), :multiple => true ) %></label></p>
<p><label>Abstract:<br/>
<%= text_field_with_auto_complete :tip, :abstract %></label></p>
<p><label>Name: <br/>
<%= text_field :tip, :name %></label></p>
<p><label>Link: <br/>
<%= text_field :tip, :link %></label></p>
<p><label>Content<br/>
<%= text_area :tip, :content, :rows => 5 %></label></p>
<p><label>Tags <span>(space separated)</span><br/>
<%= text_field_tag 'tags', #tip.tag_list, :size => 40 %></label></p>
class TipsController < ApplicationController
before_filter :authenticate, :except => %w(index show)
# GET /tips
# GET /tips.xml
def index
#tips = Tip.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #tips }
end
end
# GET /tips/1
# GET /tips/1.xml
def show
#tip = Tip.find_by_permalink(params[:permalink])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #tip }
end
end
# GET /tips/new
# GET /tips/new.xml
def new
#tip = session[:tip_draft] || current_user.tips.build
end
def create
#tip = current_user.tips.build(params[:tip])
#tipMail=params[:email]
#if tipMail
# TipMailer.deliver_email_friend(params[:email], params[:name], tip)
# flash[:notice] = 'Your friend has been notified about this tip'
#end
#tip = current_user.tips.build(params[:tip])
#tip.categories << Category.find(params[:categories]) unless params[:categories].blank?
#tip.tag_with(params[:tags]) if params[:tags]
if #tip.save
flash[:notice] = 'Tip was successfully created.'
session[:tip_draft] = nil
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def edit
#tip = Tip.find(params[:id])
end
def update
#tip = Tip.find(params[:id])
respond_to do |format|
if #tip.update_attributes(params[:tip])
flash[:notice] = 'Tip was successfully updated.'
format.html { redirect_to(#tip) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #tip.errors, :status => :unprocessable_entity }
end
end
end
def destroy
#tip = Tip.find(params[:id])
#tip.destroy
respond_to do |format|
format.html { redirect_to(tips_url) }
format.xml { head :ok }
end
end
def observe_new
session[:tip_draft] = current_user.tips.build(params[:tip])
render :nothing => true
end
end
the quick answer is that form_tag doesn't support :action as an option, you want to be passing a string as a path in. A slightly longer answer is you shouldn't be using form_tag anyways for a model edit form, you should be using form_for.
what rails are you using? .rhtml is pretty old, rails generators should be giving you .html.erb files. if it is something even remotely recent, you should be able to use
<% form_for #tip do |f| %>
<%= f.label :title, 'Title' %><br />
<%= f.text_field %>
... etc
<% end %>

Resources