I have a form as follows:
<%= form_for(:session, :url => sessions_path, :remote => true, :html => {:id => 'login_form'}) do |f| %>
<div class="formRow">
<%= f.label :email %><br>
<%= f.text_field :email, :value => (#email if #email) %>
</div>
<div class="formRow">
<%= f.label :password %><br>
<%= f.password_field :password %>
</div>
<div class="formRow small">
<%= link_to "I forgot my password",'#' %>
</div>
<div class="formRow">
<%= f.submit signin_button_text, :class => "button-big left" %>
</div>
<% end %>
It goes to this controller:
def create
#email = params[:session][:email]
user = User.authenticate(params[:session][:email],params[:session][:password])
respond_to do |format|
if user.nil?
#title = "Sign in"
flash.now[:error] = "Invalid email/password combination"
format.js {render :action => :new }
else
sign_in user
format.js {render :action => :create }
end
end
end
Here is the new.js file:
$('#login_form').replaceWith("<%=escape_javascript(render 'login_form')%>");
if($('.flash-block').length ==0) {
$('#login_form').before("<div class='flash-block error'><span><%=escape_javascript(flash[:error])%></span></div>");
}
For some reason if the form is submitted with errors it loops four times.
I don't understand why.
Is there something in the code that causes this to loop?
I am assuming that you are using jquery. This is usually happened when there is an incomplete call or there is some sort of error and you haven't refresh the page. Try something like this:
<script type="text/javascript">
$('#login_form').submit(function() {
$(this).unbind('submit').submit();
});
</script>
I was using Fancybox and was not opening it in an iframe. So I wound up loading the jquery libraries twice and thus when I submitted the form I had multiple submissions.
Once I opened Fancybox in an iframe it submitted only once.
Related
Hey Guys I am getting issue, No Route Matches, though I have created both new as well as create method.
portfollios_controller.rb
class PortfolliosController < ApplicationController
def index
#portfolio_items = Portfollio.all
end
def new
#portfolio_item = Portfollio.new
end
def create
#portfolio_item = Portfollio.new(params.require(:portfollio).permit(:title, :subtitle, :body))
respond_to do |format|
if #portfolio_item.save
format.html { redirect_to portfollio_path, notice: 'Your portfolio item is now live.' }
else
format.html { render :new }
end
end
end
end
routes.rb
Rails.application.routes.draw do
resources :portfollios
end
new.html.erb
<h1>Create a new Portfolio Item</h1>
<%= form_with(model: #portfolio_items, local: true) do |form| %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :subtitle %>
<%= form.text_field :subtitle %>
</div>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Can someone help me out with the issue. I am not able to figure it out.
I figured it out. The issue is with with the file new.html.erb
I needed to change below line:-
<%= form_with(model: #portfolio_items, local: true) do |form| %>
to
<%= form_with(model: #portfolio_item, local: true) do |form| %>
It should be #portfolio_item not plural.
I am running into issues trying to separate a form from a view into a partial. I want to use the same form for the new and edit views. These are both on the same page. The new model form is at the top of the page and uses a variable that I set in the controller.
<%= form_for #new_hire do |f| %>
<%= render :partial => 'new_hire_requests/form', :locals => {:f => f} %>
<% end %>
I then have a partial for the pending approvals that gets rendered by another partial
<%= render :partial => 'pending_approval', :collection => #pending_approval %>
And inside the pending approval partial I have this
<%= form_for pending_approval do |f| %>
<%= render :partial => 'new_hire_requests/form', :locals => {:f => f} %>
<% end %>
This is throwing an error
undefined method `new_hire_request_path' for #<#<Class:0x0000010488ac98>:0x0000010223ffc0>
Is there a way to re use the form code for both a new and edit form on the same page?
Controller Logic
#new_hire = NewHireRequest.new
#new_hire_requests = current_user.new_hire_requests
#pending_approval = #new_hire_requests.select{|p| p.status == 'pending_hr_approval' || p.status == 'pending_exec_approval'}
Partial code
<%= render 'shared/error_messages', object: f.object %>
<fieldset class="first">
<%= f.label :first_name, "First Name" %>
<%= f.text_field :first_name %>
</fieldset>
<fieldset>
<%= f.label :last_name, "Last Name" %>
<%= f.text_field :last_name %>
</fieldset>
<%= f.submit "Submit for Approval <i class='icon-share-alt icon-white'></i>",
class: "button_green" %>
add resources new_hire_requests in the routes and get done with it .
I am relatively new to programming and to rails so please be indulgent:)
I am building a website for myself which contains a blog. I have two models that are nested and I do not seem to understand how to use REST to perform certain actions on my articles and comments.
When I create a comment if the comment doesn't pass validation I want it to render the page again so that the user can correct his mistakes and resubmit the comment. When I try to render, it gives me a missing template error.
Here is the code:
You can also find this code on github --> https://github.com/MariusLucianPop/mariuslp-
routes.rb
Mariuslp::Application.routes.draw do
get "categories/new"
root :to => "static_pages#index"
match "login" => "visitors#login" # not rest
match "logout" =>"visitors#logout" # not rest
match "comment" => "articles#show"
resources :articles do
resources :comments
end
resources :tags, :taggings, :visitors, :categories, :comments
end
articles_controller.rb
def show
#article = Article.find(params[:id])
#comment = #article.comments.new
end
comments_controller.rb
def create
article_id = params[:comment].delete(:article_id)
#comment = Comment.new(params[:comment])
#comment.article_id = article_id
if #comment.save
redirect_to article_path(#comment.article_id)
else
render article_path(#comment.article_id,#comment) ## This one doesn't work
end
end
def new
#comment = Comment.new
end
def destroy
Comment.find(params[:id]).destroy
redirect_to articles_path()
end
Views-articles:
_comment.html.erb
<div class="comment">
<%= comment.body %><br />
<%= link_to "Delete Comment", article_comment_path(#article), :method => :delete, :confirm => "Are you sure you want to delete this comment?" %>
</div>
_comment_form.html.erb
<%= form_for #comment do |f|%>
<%= f.hidden_field :article_id%>
<%= f.label :body %><br />
<%= f.text_area :body, :cols => 50, :rows => 6 %><br />
<%= f.submit%>
<%end%>
show.html.erb
<p><%= link_to "<< Back to Articles", articles_path%></p>
<div class = "article_show">
<%= label_tag :category_id %>
<%= #article.category_id%> <br />
<%= label_tag :title%>:
<%= #article.title%> <br />
<%= label_tag :body%>:
<%= #article.body%> <br />
<%= label_tag :tag_list%>:
<%= #article.tag_list%><br />
</div>
<br />
<% if session[:username]== "marius"%>
<div class ="admin">
<%= link_to "Edit", edit_article_path(#article)%>
<%= link_to "Delete", article_path(#article), :method => :delete, :confirm => "Are you sure you want to delete this article ?"%>
</div>
<%end%>
<br />
<%= render :partial => 'comment', :collection => #article.comments %>
<%= render :partial => 'comment_form'%>
Have you tried to use where you point the problem?
render 'articles/show'
You don't need to use article_comment_path because that is a full path, not just the place where you store the view templates. In this case, you only need the view. Of couse you must be sure to get all instance variable which you use in this views.
UPDATE:
#article = Articles.find(article_id)
render 'articles/show'
I am trying to use form_tag to pass the params captured by the form to my users controller. I am attempting to communicate with a Sinatra server, and so I do not have a database on the client. My view is as follows:
<% form_tag(#user) do %>
<div class="field">
<%= label_tag :first_mame %><br />
<%= text_field_tag :first_name %>
</div>
<div class="field">
<%= label_tag :last_name %><br />
<%= text_field_tag :last_name %>
</div>
<div class="field">
<%= label_tag :email %><br />
<%= text_field_tag "user[email]" %>
</div>
<div class="field">
<%= label_tag :device_id %><br />
<%= text_field_tag "user[device_id]" %>
</div>
<div class="field">
<%= label_tag :type %><br />
<%= text_field_tag "user[device_type]" %>
</div>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
The create action on my controller is simply:
def create
#user = User.new(params[#user])
#user.save
respond_to do |format|
if #user.save
format.html { redirect_to(#user, :notice => 'User was successfully created.') }
format.json {render :json => #user }
format.xml { render :xml => #user, :status => :created, :location => #user }
else
format.html { render :action => "new" }
format.xml { render :xml => #user.errors, :status => :unprocessable_entity }
end
end
end
Here's what I get as a result => expected an attributes Hash, got nil
Anybody know why? Thanks for the help.
You need to use form_for and not form_tag. form_for(#user) do
In your model you need to create a schema. Without it Rails doesn't know what do with the data you enter into the form.
When you pass the object into the parameter hash use :user and not #user. #user = User.new(params[:user])
For your form you need to do
<%= form_for #user do |f| %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name %>
</div>
# more fields
<% end %>
Note the:
<% %> --> <%= %>
form_tag(#user) do --> form_for(#user) do |f|
label_tag --> f.label
text_field_tag --> f.text_field
In you controller:
#user = User.new(params[:user])
Update:
<% %> --> <%= %>: This is just the convention in rails3, when ever you want to write something in the response you should use later(with = sign). Earlier still works but is deprecated.
form_tag(#user) do --> form_for(#user) do |f|
form_tag(#user) do: form_tag is used to for simple forms which are not tied with any model. You can have the tags inside form_tag named so that they resemble form for, but then why wouldn't you use form_for directly. Apparently the first parameter to the helper is target url, and in this particular case rails magically identifies the url from #user and you didn't notice any bug
form_for(#user) do |f|: form_for is used to create a form for a model, and ties up the form with the instance of the model passed to it. The block for form_for receives a form_builder object, which has equivalents of text_field_tag, label_tag etc. as text_field, label
label_tag --> f.label: first is the common tag which just creates a label tag with no magic attached to it.The later is related with the model object and follows different naming and id conventions, than former. It also ties up with the value of the field, i.e. if the field has an error(failed validation), your label will be surrounded by a div tag with class fields_with_error or something, I can't remember the class name.
text_field_tag --> f.text_field: Former will create a field with name first_name with no magic attached. The later follows a naming convention, the input field will be named user[first_name], so that when you do params[:user] you get a first_name parameter there. It also ties up with the value of the field with the html input, i.e. you get the same error functionality as label and you also get the input automatically prefilled with whatever the value field has in the model instance.
Basically whats happening is I can create a new item that gets saved to my table in my db. but when I go to edit the item, the form opens up, I make the change and then when I go to submit, it takes me to the same url as the edit page and gives me Routing Error No route matches "/support/14/edit" although if you enter that in the address bar it opens the edit form just fine, but doesn't have any of my changes saved. So here is my code.
routes.rb
resources :support
support_controller.rb
def new
#support_item = Support.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #support_item }
end
end
# GET /support/1/edit
def edit
#support_item = Support.find(params[:id])
end
# POST /support
# POST /support.xml
def create
#support_item = Support.new(params[:support_item])
respond_to do |format|
if #support_item.save
format.html { redirect_to("/support", :notice => 'Question was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
# PUT /support/1
# PUT /support/1.xml
def update
#support_item = Support.find(params[:id])
respond_to do |format|
if #support_item.update_attributes(params[:support_item])
format.html { redirect_to("/", :notice => 'Question was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #support_item.errors, :status => :unprocessable_entity }
end
end
end
support.rb
class Support < ActiveRecord::Base
belongs_to :role
scope :admin_available, order("role_id ASC") do
Support.all
end
def self.available(user)
questions = where(:role_id => 1)
questions += where(:role_id => user.roles)
questions
end
end
_form.html.erb
<% if #support_item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#support_item.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #support_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Support item for:" %><br />
<%= f.collection_select :role_id, Role.find_by_max(5), :id, :name, {:default => 'everyone'} %>
</div>
<div class="field">
<%= f.label :question %><br />
<%= f.text_field :question, :class => 'genForm_question'%>
</div>
<div class="field">
<%= f.label :answer %><br />
<%= f.text_area :answer, :class => 'genForm_textarea' %>
</div>
<div class="field">
<%= f.label :url %><br />
<%= f.text_field :url, :class => 'genForm_question' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
new.html.erb
<h1>New Support Item</h1>
<% form_for #support_item, :url => { :action => "create" }, :html => { :method => :post } do |f| %>
<%= render 'form', :f => f %>
<% end %>
edit.html.erb
<h1>Editing Support Item</h1>
<% form_for #support_item, :url => { :action => "edit" }, :html => { :method => :post } do |f| %>
<%= render 'form', :f => f %>
<% end %>
I believe thats all the relavent code.
<h1>Editing Support Item</h1>
<% form_for #support_item do |f| %>
<%= render 'form', :f => f %>
<% end %>
You are overriding the URL. It should be able to be auto-generated like that if you are doing everything with standard rest. If that doesn't work out, just know you don't want to submit to /support_items/1/edit, you want to submit to /support_items/1.