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 %>
Related
I have a strange NoMethodError that's appearing when a form fails validation in Rails. I can access the 'new' method without a problem to fill out the form the first time. But then the form submits to a nested controller and if validation fails, it tried to redirect to 'new' again (as it should) but throws undefined method articles_path.
I thought it might be to do with the nesting, but I believe the setup here is correct for it to load the nested 'new' path?
I'm sure I'm missing something obvious... But why is this happening?
Controller:
def new
#user = User.find(current_user)
#article = Article.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #article }
end
end
def create
#article = current_user.articles.build(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to root_path, :flash => { :notice =>
"Article added!" } }
else
format.html { render :action => "new" }
format.xml { render :xml => #article.errors, :status =>
:unprocessable_entity }
end
end
end
Form:
= form_for([#user, #article], :html => { :multipart => true }) do |f|
- if #article.errors.any?
#errorExplanation
%h2
= pluralize(#article.errors.count, "error")
prohibited this post from being saved:
%ul
- #article.errors.full_messages.each do |msg|
%li= msg
.clearfix
= f.label :title, "Article title *"
.input
= f.text_field :title
You're right that the error is caused by nested resource, particularly this part of the code
= form_for([#user, #article], :html => ...
When validation fails, #user is nil which causes the url to look like [nil, #article] which turns into articles_path if #article is not persisted. So the solution is to set #user in the create action.
I scaffolded an Authorization(user_id, code, otherparam1, ...) and I want to add a custom_create method to the authorizations_controller which only require a code, the method generating the other parameters. I need this method to work with json or be called in others methods like User.newcustom(code)
def newcustom
case request.method
when :post #post
code = params[:code]
if(code and code != '')
...
respond_to do |format|
if #authorization.save
format.html { redirect_to #authorizations, notice: 'Authorization was successfully created.' }
format.json { render json: #authorizations, status: :created, location: #authorization }
else
format.html { render action: "new" }
format.json { render json: #authorization.errors, status: :unprocessable_entity }
end
end
else #get
respond_to do |format|
format.html
end
end
Here's my newcustom.html.erb
<%= form_tag( newcustom_authorizations_path, :method => :post ) do %>
<div class="field">
<%= text_field_tag :code %>
</div>
<div class="actions">
<%= submit_tag('Get tokens') %>
</div>
<% end %>
But it doesn't work through json, the form. And calling the method newcustom(:code => code) throws me a too many arguments (1 for 0). Any idea ?
for this add the given code to config/routes.rb
resources :authorizations do
collection do
post :newcustom
end
end
Thanks for the help everyone.
I ended up solving my problem filtering the parameters of the create function, then calling my customcreate one.
AND YES I CHEAT :)
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.
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.
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