Keeping devise flash message only - ruby-on-rails

Hello i have a quick question. I get 2 flash messages, the devise one and the scaffolded one. How can i just have the Devise message in the beautiful flash box only?
Here is my code for creating Products:
def create
#product = current_user.products.build(params[:product])
#product.user = current_user
respond_to do |format|
if #product.save
format.html { redirect_to(#product, :notice => 'Product was successfully created.') }
format.xml { render :xml => #product, :status => :created, :location => #product }
else
format.html { render :action => "new" }
format.xml { render :xml => #product.errors, :status => :unprocessable_entity }
end
end
end
products/show.html.erb:
<p id="notice"><%= notice %></p>
<p><b>Name:</b><%= #product.name %> </p>
<p><b>Date:</b><%= #product.date %></p>
<p><b>Price:</b><%= number_to_currency(#product.price) %></p>
<p><b>User:</b><%= #product.user_id %></p>
<p><b>Latitude:</b><%= #product.latitude %></p>
<p><b>Longitude:</b><%= #product.longitude %></p>
<p><b>Tags:</b> <%= #product.tag_list %></p>
<%= link_to 'Edit', edit_product_path(#product) %> |
<%= link_to 'Back', products_path %>
Thank you!

Do you have a 'notice' in your application.html.erb as well?
<p id="notice"><%= notice %></p>
It may be best to put your notice & alert in there, instead of in all of your views anyway. Always good to refactor.

I think devise is sending a flash measage because of the way you are building a product. Instead you might try this:
def create
#product = Product.new(params[:product])
#product.user = current_user
respond_to do |format|
if #product.save
format.html { redirect_to(#product, :notice => 'Product was successfully created.') }
format.xml { render :xml => #product, :status => :created, :location => #product }
else
format.html { render :action => "new" }
format.xml { render :xml => #product.errors, :status => :unprocessable_entity }
end
end
end

Related

Rails trying to save form with collection_select that is empty gives undefined method `map' for nil:NilClass

I have a form with a drop down generated from a collection_select. The collection_select starts off blank and then when the user selects a date from the date field, the collection_select is updated with values for the date that is chosen.
I'm trying to show a nice error message if the form is submitted without a value chosen in my dropdown. Currently i'm getting this error: undefined methodmap' for nil:NilClass`
How can i make it so that if the user doesn't select a value in the dropdown, I can show them a nice error message?
View
<%= form_for(#arrangement) do |f| %>
<div class="control-group">
<%= f.label :date, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :date, :class => 'input-large datepicker' %>
</div>
</div>
<div class="control-group">
<%= f.label :timeslot, :class => 'control-label' %>
<div class="controls">
<%= collection_select(:arrangement, :timeslot_id, #available_timeslots, :id, :timeslot_spelled) %>
</div>
</div>
<% end %>
Controller
# GET /arrangements/new
# GET /arrangements/new.json
def new
date = Time.now.to_date.to_s(:db)
#arrangement = Arrangement.new
#available_timeslots = Timeslot.where("location_id = ? AND date(timeslot) = ? AND arrangement_id is null", current_user.user_details.location_id, date).order('timeslot ASC')
respond_to do |format|
format.html # new.html.erb
format.json { render json: #arrangement }
end
end
# POST /arrangements
# POST /arrangements.json
def create
#arrangement = Arrangement.new(params[:arrangement])
respond_to do |format|
if #arrangement.save
# update the timslot record with the arrangement id
if #timeslot = Timeslot.update(#arrangement.timeslot_id, :arrangement_id => #arrangement.id)
format.html { redirect_to #arrangement, notice: 'Arrangement was successfully created.' }
format.json { render json: #arrangement, status: :created, location: #arrangement }
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
end
end
The error given is referring to #available_timeslots being empty when trying to save the form
I would try something like this.
def new
#available_timeslots = ...
if #available_timeslots.count > 0
flash.now[:error] = "nil errrraaarrr"
end
...
end
In view
<div class="controls">
<%- if #available_timeslots.count > 0 %>
<%= collection_select(:arrangement, :timeslot_id, #available_timeslots, :id, :timeslot_spelled) %>
<% else %>
<%= flash.now[:error] %>
<% end %>
</div>
#available_timeslots is nil. Make sure it's set with an array of available timeslots to avoid this error message.
The trick was to add #available_timeslots = [] in the else clause
def create
#arrangement = Arrangement.new(params[:arrangement])
respond_to do |format|
if #arrangement.save
# update the timslot record with the arrangement id
if #timeslot = Timeslot.update(#arrangement.timeslot_id, :arrangement_id => #arrangement.id)
format.html { redirect_to #arrangement, notice: 'Arrangement was successfully created.' }
format.json { render json: #arrangement, status: :created, location: #arrangement }
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
end
end

Assigning multiple associations with Simple_form

I understand how to implement a single has_many association using simple_form, but how do you assign an additional association from another model object?
In my code, I'm creating model object #opportunity. I'm currently assigning a company_id, but also need to assign a 'user_id.
#opportunity _form.html.erb
<% if user_signed_in? %>
<%= simple_form_for([#company, #company.opportunities.build], html: {class: "form-inline"}) do |f| %>
<%= f.error_notification %>
<%= f.input :description, label: false, placeholder: 'Create an opportunity', input_html: { class: "span4" } %>
<%= f.submit 'Submit', class: 'btn btn-small'%>
<% end %>
<% else %>
<%= link_to "Create an Account", new_user_registration_path %>
to contribute
<% end %>
opportunity_controller.rb
def create
#company = Company.find(params[:company_id])
#opportunity = #company.opportunities.create(params[:opportunity])
respond_to do |format|
if #opportunity.save
format.html { redirect_to company_path(#company), notice: 'Opportunity was successfully created.' }
format.json { render json: #opportunity, status: :created, location: #opportunity }
else
format.html { render action: "new" }
format.json { render json: #opportunity.errors, status: :unprocessable_entity }
end
end
end
Assuming that your user is logged in, you can change your controller action to the following:
def create
#company = Company.find(params[:company_id])
#opportunity = #company.opportunities.new(params[:opportunity]) # new instead of create
#opportunity.user = current_user # new
respond_to do |format|
if #opportunity.save
format.html { redirect_to company_path(#company), notice: 'Opportunity was successfully created.' }
format.json { render json: #opportunity, status: :created, location: #opportunity }
else
format.html { render action: "new" }
format.json { render json: #opportunity.errors, status: :unprocessable_entity }
end
end
end

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 %>

Partial Rendering of another controller's view ruby on rails

I am using twitter Boostraps tabbable feature found here:
http://twitter.github.com/bootstrap/components.html#navs
And within this navigation content window, I am trying to render a view that displays a "course". This view found in views/courses/_show.html.erb looks like this:
<div class="center hero-unit">
<h1><%= #course.course_name %></h1>
<%= link_to 'New Question Set',new_answer_path(:course_ID => #course.id), :class => "btn btn-large btn-primary" %>
<%= link_to 'Launch Session!', edit_course_path, :class => "btn btn-large btn-primary" %>
</div>
I am trying to render it and failing with the following code in views/instructor/show.html.erb
<% courses.each do |c| %>
<div class="tab-pane" id="<%=c.course_name%>">
<%= render :partial => 'courses/show', :locals => {#course=>c} %>
</div>
I get the following error:
/app/views/courses/_show.html.erb:1: syntax error, unexpected '=',
expecting keyword_end ...tput_buffer = #output_buffer; =
local_assigns[:];show = loca... ... ^
/app/views/courses/_show.html.erb:1: syntax error, unexpected ']',
expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or
tSTRING_END ...tput_buffer; = local_assigns[:];show =
local_assigns[:show];... ...
saying it's failing at line 1 of my courses/_show.html.erb
My Course Controller looks like this:
class CoursesController < ApplicationController
# GET /courses
# GET /courses.json
def index
#courses = Course.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #courses }
end
end
# GET /courses/1
# GET /courses/1.json
def show
#course = Course.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #course }
end
end
# GET /courses/new
# GET /courses/new.json
def new
#course = Course.new(instructor_ID: params[:instructor_ID])
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #course }
end
end
# GET /courses/1/edit
def edit
#course = Course.find(params[:id])
end
# POST /courses
# POST /courses.json
def create
#course = Course.new(params[:course])
respond_to do |format|
if #course.save
format.html { redirect_to #course, :notice => 'Course was successfully created.' }
format.json { render :json => #course, :status => :created, :location => #course }
else
format.html { render :action => "new" }
format.json { render :json => #course.errors, :status => :unprocessable_entity }
end
end
end
# PUT /courses/1
# PUT /courses/1.json
def update
#course = Course.find(params[:id])
respond_to do |format|
if #course.update_attributes(params[:course])
format.html { redirect_to #course, :notice => 'Course was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #course.errors, :status => :unprocessable_entity }
end
end
end
Note: I have ommited some of the methods like delete in my controller to save space.
Any Ideas?!
In #course=>c change the # to a colon.

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.

Resources