Rails form_for own action routes - ruby

I want to modify the action (submit) for a form_for helper
<%= form_for(#rating, :as => :post, :url => demo_create_rating_path(#rating)) do |f| %>
<div class="field">
<%= f.label :value %><br />
<%= f.select :value, %w(1 2 3 4 5) %>
</div>
<%= f.hidden_field :article_id, :value => #article.id%>
<%= f.hidden_field :user_id, :value => current_user.id %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description, size: "100x5" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is my View and it does not work.
All I want is, that I can redirekt the Action after submit button but then a get this error:
ActionController::RoutingError (No route matches {:controller=>"demo_ratings", :action=>"create", :article_id=>#<Rating id: nil, value: nil, description: nil, article_id: nil, user_id: nil, created_at: nil, updated_at: nil>}):
app/views/demo_ratings/_form.html.erb:1:in `_app_views_demo_ratings__form_html_erb__1912848844925280312_70155649546120'
app/views/demo_ratings/new.html.erb:13:in `_app_views_demo_ratings_new_html_erb__27525029454473720_70155632487040'
What am I doing wrong?
UPDATE
All my funktion that are need by form_for helper:
def new
#rating = Rating.new
#article = Article.find(params[:article_id])
end
def edit
#rating = Rating.find(params[:id])
#article = Article.find(params[:article_id])
end
def create
#rating = Rating.new(params[:rating])
if #rating.save
#article= Article.find(params[:article_id])
puts #article.name
puts #rating.id
#rating.article = #article
puts #rating.article.name
redirect_to demo_rating_path(#rating, :article_id => #article.id), notice: 'Rating was successfully created.'
else
render action: "new"
end
end
def update
#rating = Rating.find(params[:id])
if #rating.update_attributes(params[:rating])
#article = #rating.article
redirect_to demo_rating_path(#rating), notice: 'Rating was successfully updated.'
else
render action: "edit"
end
end

Try this:
<%= form_for(#rating, :as => :post, :url => demo_create_rating_path) do |f| %>
The #rating in the url is providing a nil object id, and you don't have an id yet.
If you want to share the form between create and update, then use the following:
<% form_for(#rating, :as => :post) do |f| %>
For reference, review the output of a rails generated scaffold's _form.html.erb.
In your controller, your are saving the new/ updated record before your processing. The statement if #rating.save should come after #rating.article = #article.
def create
#rating = Rating.new(params[:post])
#article= Article.find(params[:article_id])
#rating.article_id = #article.id
if #rating.save
redirect_to demo_rating_path(#rating, :article_id => #article.id), notice: 'Rating was successfully created.'
else
render action: "new"
end
end

Related

Always showing error message on the form with Ruby on Rails

I'm facing this problem how to fix this issue?
This is my problem, always showing an error message:
This is my model:
validates :first_name, :presence => true, :length => { :in => 3..20 }
validates :last_name, :presence => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, format: { with: /\A[^#\s]+#([^#.\s]+\.)+[^#.\s]+\z/ }
# validates :passkey, :confirmation => true #password_confirmation attr
validates_length_of :passkey, :presence => true, :in => 6..20, :on => :register
This is my Controller:
def register
params.permit!
#jobseekers = Jobseeker.new(params[:jobseekers])
if #jobseekers.save
redirect_to home_path
else
render "register"
end
end
This is my view:
<%= form_for :jobseekers, url: register_path(#jobseekers), method: :patch do |f| %>
<%= f.text_field :first_name, placeholder: 'First Name'%>
<%= f.text_field :last_name, placeholder: 'Last Name'%>
<%= f.text_field :email, placeholder: 'Email'%>
<%= f.password_field :passkey, placeholder: 'Password'%>
<%= f.submit "Create Account" %>
<% end %>
<% if #jobseekers.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#jobseekers.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% #jobseekers.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
I need show this error after click submit button, but now showing this error always.
Please help!
Now I'm using ruby 1.9.7 rails 4.2.5 & mysql2
The problem in your code is the controller action:
def register
params.permit!
#jobseekers = Jobseeker.new(params[:jobseekers])
if #jobseekers.save
redirect_to home_path
else
render "register"
end
end
if you take a look, then you don't just create a new object #jobseekers but try to save it right away. if #jobseekers.save will be false because the params[:jobseekers] is empty when you call the action for the first time. Since you have validations, it will automatically add errors, and your register view will be rendered with those errors. This is why usually there are 2 actions with forms- new and create.
To fix this split this into two actions
def register
#jobseekers = Jobseeker.new
end
def create_register
params.permit!
#jobseekers = Jobseeker.new(params[:jobseekers])
if #jobseekers.save
redirect_to home_path
else
render "register"
end
end
where create_register only has a post route and register has a get route (register will render the form for the first time, and then after submit the create_register will be called and render the form with errors if there are any)
This would then be your view(stating the method is not necessary in this case):
<%= form_for :jobseekers, url: create_register_path(#jobseekers) do |f| %>
<%= f.text_field :first_name, placeholder: 'First Name'%>
<%= f.text_field :last_name, placeholder: 'Last Name'%>
<%= f.text_field :email, placeholder: 'Email'%>
<%= f.password_field :passkey, placeholder: 'Password'%>
<%= f.submit "Create Account" %>
<% end %>
<% if #jobseekers.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#jobseekers.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% #jobseekers.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
and your routes.rb entries:
get '/register', to: "jobseekers#register"
post '/register', to: "jobseekers#create_register"
Another take on Kkulikovskis' answer would be:
def register
params.permit!
#jobseekers = Jobseeker.new(params[:jobseekers])
return true if request.get? # <--- skip validations if not on form submit
if #jobseekers.save
redirect_to home_path
else
render "register"
end
end
I prefer this approach when the instantiation of #jobseeker is less trivial
PS params.permit! is a path to hell, don't do it

undefined method `Password_field' error using Rail3

I am getting the following error while trying to do reset password by sending sending the link to email and open that link from that email.
Error:
NoMethodError in Admins#editpass
Showing C:/Site/swargadwar_admin/app/views/admins/editpass.html.erb where line #16 raised:
undefined method `Password_field' for #<ActionView::Helpers::FormBuilder:0x21c0108>
Extracted source (around line #16):
13: <% end %>
14: <p>
15: <label for "new_pass">New Password :</label>
16: <%= f.Password_field :password,placeholder:"Enter your new password" %>
17: </p>
18: <p>
19: <label for "new_pass">Confirm New Password :</label>
Rails.root: C:/Site/swargadwar_admin
Application Trace | Framework Trace | Full Trace
app/views/admins/editpass.html.erb:16:in `block in _app_views_admins_editpass_html_erb___904659562_17338176'
app/views/admins/editpass.html.erb:2:in `_app_views_admins_editpass_html_erb___904659562_17338176'
Please check my below codes and let me to know where i did the mistake as well as try to help me to resolve this.
views/admins/editpass.html.erb
<center>
<%= form_for :admin,:url => {:action => "setpass",:id => params[:id] } do |f| %>
<% if #admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#admin.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<label for "new_pass">New Password :</label>
<%= f.Password_field :password,placeholder:"Enter your new password" %>
</p>
<p>
<label for "new_pass">Confirm New Password :</label>
<%= f.Password_field :password_confirmation,placeholder:"confirm your new password" %>
</p>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
</center>
controller/admins_controller.rb
class AdminsController < ApplicationController
def create_registration
#admin=Admin.new(params[:admin])
if #admin.save
flash[:notice]="User has created successfully"
flash[:color]="valid"
redirect_to :action => "index" , :controller => 'homes'
else
flash[:alert]="User could not created"
flash[:color]="invalid"
render 'homes/index'
end
end
def forget
#admin=Admin.new
end
def resetpass
#admin=Admin.find_by_email(params[:admin][:email])
if #admin.email==params[:admin][:email]
UserMailer.registration_confirmation(#admin).deliver
flash[:notice]="Check your email to reset the password"
flash[:color]="valid"
redirect_to :action => "index" , :controller => 'homes'
else
flash[:alert]="Invalid email id"
flash[:color]="invalid"
render 'homes/index'
end
end
def editpass
#admin=Admin.new
end
def setpass
#admin=Admin.find(params[:id])
if #admin.update_attributes(params[:admin])
flash[:notice]="Your password has updated successfully"
flash[:color]="valid"
redirect_to :action => "index" , :controller => 'homes'
else
flash[:alert]="Your password could not updated"
flash[:color]="invalid"
render 'homes/index'
end
end
end
model/admin.rb
class Admin < ActiveRecord::Base
attr_accessible :email, :password_hash, :password_salt, :picture, :user_name,:password_confirmation,:password, :remember_me
attr_accessor :password
attr_accessor :remember_token
before_save :encrypt_password
mount_uploader :picture, PictureUploader
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :user_name, :presence => true, :length => {:in => 3..10}
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
has_secure_password
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def Admin.digest(string)
cost = 10
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def Admin.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = Admin.new_token
update_attribute(:remember_digest, Admin.digest(remember_token))
end
def forget
update_attribute(:remember_digest, nil)
end
end
Please help me.
You just have a typo: The method is named password_field, not Password_field. Just change the two method calls in app/views/admins/editpass.html.erb and you are done.

How to resolve NoMethodError in Users#edit in ror

Please anyone help me to resolve the following error.Actually I want to show the exit values those present in DB on the text field in edit page.But i am getting some error.
Error:
NoMethodError in Users#edit
Showing C:/Site/edit/app/views/users/edit.html.erb where line #4 raised:
undefined method `each' for #<User:0x3ac9ea8>
Below are my code snippets
views/users/index.html.erb:
<h1>This is index page</h1>
<center>
<p>Enter data</p>
<div class="option">
<p><%= link_to "Click here to enter data",users_new_path %></p>
<p><%= link_to "Display data",users_show_path%></p>
</div>
</center>
views/users/show.html.erb
<h1>Show your data</h1>
<center>
<ul>
<% #user.each do |t| %>
<li>
<%= t.name %> |
<%= t.email %> |
<%= t.password%> |
<%= t.created_at %>
<%= link_to "edit",users_edit_path(:id => t.id) %> || <%= link_to "Reset Password",users_reset_path(:id => t.id) %>
</li>
<% end %>
</ul>
<div class="back_btn">
<button type="button" class="btn-custom " style="cursor:pointer;">Back</button>
</div>
</center>
views/users/edit.html.erb
<h1>Edit your data here</h1>
<center>
<%= form_for #user ,:url => {:action => "update",:id => params[:id]} do |f| %>
<% #edit.each do |t| %>
<div class="div_reg">
<p>
<label for="username" class="uname" data-icon="u" >username </label>
<%= f.text_field:name, :input_html => {:value => t.name}%>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Email </label>
<%= f.text_field:email,:input_html => {:value => t.email} %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= f.password_field:password,:input_html => {:value => t.password} %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= f.password_field :password_confirmation %>
</p>
<center>
<%= f.submit "Update",:class => 'btn-custom' %>
</center>
<div class="back_btn">
<button type="button" class="btn-custom " style="cursor:pointer;">Back</button>
</div>
</div>
<% end %>
<% end %>
</center>
<% if #user.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in #user.errors.full_messages %>
<li><%= message_error %></li>
<% end %>
</ul>
<% end %>
controller/users_controller.rb
class UsersController < ApplicationController
def index
end
def new
#user=User.new
end
def create
#user=User.new(users_param);
if #user.save
flash[:notice]="You signed up successfully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="You have not signed up successfully"
flash[:color]="invalid"
redirect_to :action => 'new'
end
end
def show
#user=User.all
end
def edit
#user=User.new
#edit=User.find(params[:id])
end
def update
flash[:notice]=params[:id]
#user=User.find(params[:id])
if #user.update_attributes(update_params)
flash[:notice]="Your data is updated succesfully"
flash[:color]="valid"
redirect_to :action => 'show'
else
flash[:alert]="Your data could not update,Please check it..!!"
flash[:color]="invalid"
redirect_to :action => 'edit'
end
end
def reset
#user=User.new
end
def emailsend
#user=User.find(params[:id])
if #user.email== params[:user][:email]
UserMailer.registration_confirmation(#user).deliver
flash[:notice]="Check your email to reset the password"
flash[:color]="valid"
redirect_to :action => 'reset'
else
flash[:notice]="Check your valid email or your email is not found"
flash[:color]="invalid"
redirect_to :action => 'show'
end
end
def resetpass
#user=User.new
end
def passres
#user=User.find_by_email(params[:user][:email])
if #user.update_attributes(updates_password)
flash[:notice]="Your password id updated succefully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="Your data could not update..Please check it..!!"
flash[:color]="invalid"
redirect_to :action => 'show'
end
end
private
def users_param
params.require(:user).permit(:name, :email, :password,:password_confirmation)
end
def update_params
params.require(:user).permit(:name,:email,:password,:password_confirmation)
end
def updates_password
params.require(:user).permit(:email,:password,:password_confirmation)
end
end
model/user.rb
class User < ActiveRecord::Base
#attr_accessor :password
#attr_accessor :name
#attr_accessor :email
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
end
Please help me to resolve this error.Thanks in advance.
#edit.each is your problem. You have a User instance here not an ActiveRecord array of Users therefore .each isn't available.
You can remove the iteration <% #edit.each do |t| %> and change all references t to #edit ie. #edit.name.

Adding button click counter in rails 3

I want to add buttons on my article so that I can know the number of times its clicked and update counter on the database, I am using mongoid ,my model is:
class Article
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
field :content, :type => String
field :likes, :type => Integer ,:default => 0
field :dislikes, :type =>Integer, :default => 0
field :spam, :type => Integer, :default => 0
end
My articles show controller is:
def show
#article = Article.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #article }
end
end
My View for show is:
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b>
<%= #article.title %>
</p>
<p>
<b>Content:</b>
<%= raw #article.content %>
</p>
Likes : <%= #article.likes %> <br/>
Dislikes : <%= #article.dislikes %><br/>
Spams : <%= #article.spam %><br/>
<%= link_to 'Edit', edit_article_path(#article) %> |
<%= link_to 'Back', articles_path %>
I find anything about it in internet.
How can I achieve it?
The easiest thing to do would be add a click_count integer attribute to your Article model and then increment this in your controller code:
def show
#article = Article.find(params[:id])
#article.increment! :click_count
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #article }
end
end
i got it done, phew!
I added the following form in my show.html.erb:
<%=form_for(#article,:action=>"update") do |f| %>
<%= submit_tag "Like", :name=>"like"%>
<%= submit_tag "Dislike",:name=>"dislike"%>
<%= submit_tag "Spam",:name=>"spam" %>
<%end%>
and wrote the following update controller:
def update
#article=Article.find(params[:id])
if params[:like]
#article.likes=#article.likes+1
elsif params[:dislike]
#article.dislikes=#article.dislikes+1
elsif params[:spam]
#article.spams=#article.spams+1
end
respond_to do |format|
if #article.update_attributes(params[:article])
format.html {redirect_to #article, :notice => "Article Updated"}
else
format.html {render :action=>"edit", :notice=> "Unable to update Article , sorry! :("}
end
end
end
It worked like a charm.

accepts_nested_attrinutes_for rails3.1rc4

I use accepts_nested_attributes_for in a model but my child form isn't saved in the database?
I am building a nested form almost in the same way as in episode 196 / 197 from Ryan Bates railscasts. I have a parent question form and as a child the answer form:
models/question.rb
class Question < ActiveRecord::Base
belongs_to :user
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:text].blank? },
:allow_destroy => true
validates :content, :presence => true
end
models/answer.rb
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
end
controllers/questions_controller.rb
class QuestionsController < ApplicationController
before_filter :authenticate_user!
def index
setup_questions
end
def create
#question = Question.new(params[:question])
#this is to get every id_key from the user into the params of the answer
params[:question][:answers_attributes].keys.each {|key| params[:question][:answers_attributes][key][:user_id] = current_user.id }
#question.user_id = current_user.id
if #question.save
redirect_to questions_path, :notice => "Successfully created question."
else
setup_questions
render :index
end
end
def edit
#question = Question.find(params[:id])
end
def update
#question = Question.find(params[:id])
respond_to do |format|
if #question.update_attributes(params[:question])
format.html { redirect_to(#question, :notice => 'Question was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #question.errors, :status => :unprocessable_entity }
end
end
end
def show
#question = Question.find(params[:id])
end
def destroy
#question = Question.find(params[:id])
#authorize! :destroy, #question
#question.destroy
redirect_to questions_path, :notice => "Successfully deleted question: #{#question.content}."
end
private
def setup_questions
#questions = Question.all
#question ||= Question.new
#question.answers.build #to build the answers form
end
end
views/question/_form.html.erb
<%= form_for(#question) do |f| %>
<!-- =================== -->
<!-- = Error handeling = -->
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<!-- =================== -->
<!-- ================= -->
<!-- = Question form = -->
<div class="field">
<%= f.label :content, "Question" %><br />
<%= f.text_field :content, :placeholder => "type your question here.." %>
</div>
<!-- ====================== -->
<!-- = Nested Answer form = -->
<div class="answer-field">
<%= f.fields_for :answers do |builder| %>
<%= builder.label :content, "Possible answer" %><br />
<%= builder.text_field :content, :placeholder => "type an optional answer.." %>
<% end %>
</div>
<!-- ====================== -->
<div class="actions">
<%= f.submit %>
</div>
<!-- ================= -->
<% end %>
So now the big question... Why doesn't it save anything from the answer field to the database? I thought one create action at the parent (question) should be enough and that the "accepts_nested_attributes_for" should take care of it's child(s).
Regards,
Thijs
1 Remove this line from controller
params[:question][:answers_attributes].keys.each {|key| params[:question][:answers_attributes][key][:user_id] = current_user.id }
2 add this into your view
<!-- = Nested Answer form = -->
<div class="answer-field">
<%= f.fields_for :answers do |builder| %>
<% builder.object.user = current_user %>
<%= builder.label :content, "Possible answer" %><br />
<%= builder.text_field :content, :placeholder => "type an optional answer.." %>
<%= builder.hidden_field, :user_id %>
<% end %>
</div>
<!-- ====================== -->

Resources