I have view which contains a search form like below:
<div>
<%= form_tag :action => 'search' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :class => 'nil' %>
</p>
<% end %>
</div>
whenever i am clicking search button it is calling a new view which i dont want.
I want to be in the same page to do some action and display the result there only.
Can any one direct me how to do this?
I found the solution from the following blog
http://www.alfajango.com/blog/rails-3-remote-links-and-forms/
if your search form is in index.html.erb and you want the search result on the same page then
in your controller
def index
if params[:search]
# search query
else
# listing records
end
end
in view
replace <%= form_tag :action => 'search', :method => :get do %>
with <%= form_tag :action => 'index' do %>
Related
I am using a single form for new and edit actions in my controller.
edit.html.erb
<%= form_for #user, :url => edit_users_path(#user) do |f| %>
<%= render :partial => 'form', :object => f %>
<%= f.submit "Update" %>
<% end %>
new.html.erb
<%= form_for #user, :url => users_path do |f| %>
<%= render :partial => 'form', :object => f %>
<%= f.submit "Submit" %>
<% end %>
_form.html.erb
<%= form.text_field :firstname %>
<%= form.text_field :lastname %>
<%= form.email_field :email %>
For boths the actions, submit doesn't work. The form is rendered fine. I added data from rails console to check for edit. It doesn't work either.
If I create 2 separate form for each, new & edit, it works fine. So i assume my controller code is fine.
Thanks
Render the partial with:
<%= render :partial => 'form', :form => f %>
instead. If it doesn't work, check the input names in the generated HTML.
I see in your edit and new files are using an |f| variable for the form builder but in the form partial you're using form as the variable. Try changing the fields in your partial to "f.text_field"
I have a Rails 3 application with a view that is irritating me. The view is essentially four tabs. Each tab shows a filtered list of records like this:
Tab One
<% #faults.each do |fault| %>
<%= fault.name %>
<% end %>
Tab Two
<% #faults_pending.each do |fault| %>
<%= fault.name %>
<% end %>
Tab Three
<% #faults_closed.each do |fault| %>
<%= fault.name %>
<% end %>
Obviously there is a lot more code that I have missed out. To tidy the view up significantly I considered making a partial for each tab, however, each of those partials would be identical except for the <% #faults.each do |fault| %> line.
Is there a way of having one partial and somehow when rendering the partial set the scope (or method, I can never remember what it's called).
I think you're looking for render's :locals option.
Tab one
<%= render :partial => "faults", :locals => { :faults => #faults } %>
Tab two
<%= render :partial => "faults", :locals => { :faults => #faults_pending } %>
Tab three
<%= render :partial => "faults", :locals => { :faults => #faults_closed } %>
Partial
<% faults.each do |fault| %>
<%= fault.name %>
<% end %>
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 .
When the view pass the parameters to controller,
controller gets nil for all of the arguements somehow.
Can anyone how to fix this?? Thanks!
and I have no model called "Message"
controllers/messages_controller.rb
def deliver
recipient = User.find_by_username(params[:recipient])
subject = params[:subject]
body = params[:body]
current_user.send_message(recipient, body, subject)
redirect_to :controller => 'messages', :action => 'received'
flash[:notice] = "message sent!"
end
views/messages/new.html.erb
<%=form_for :messages, url: url_for( :controller => :messages, :action => :deliver ) do |f| %>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
<% end %>
Check your source HTML to better understand what FormHelpers do.
With the form_for f.text_field will generate names attributes in the format:
messages[subject]
Consequently, your params will be in the format:
params[:messages][:subject]
You can also use <%= debug params %> to inspect what's in params, it's very helpful.
You can get parameter value using datas = params[:messages]
These values are in array form. So you can fetch array datas If you want to individual data then usesubject = datas[:subject]
body = datas[:body]
To check run following code in view
<%= subject %>
this gives the value of subject.
I have a Post which can have multiple Tags, each of which relates to a User (think Facebook tagging).
In my Post form I have this Formtastic code:
<%= f.inputs :class => 'tags' do %>
<ul>
<%= f.semantic_fields_for :tags do |t| %>
<% if t.object.new_record? %>
<%= t.input :user_id, :label => " ", :input_html => { :class => 'chosen', :'data-placeholder' => 'Select connection' }, :as => :select, :collection => current_user.connections %>
<% end %>
<% end %>
</ul>
<% if #post.tags.present? && !#post.new_record? %>
<ul class="existing-tags">
<%= f.fields_for :tags do |t| %>
<% unless t.object.new_record? %>
<li>
<%= link_to avatar(t.object.user), user_path(t.object.user) %>
<%= t.check_box :_destroy %>
<%= t.label :_destroy, 'Remove' %>
</li>
<% end %>
<% end %>
</ul>
<% end %>
<% end %>
As you can see this can allow a tag to be added one at a time. However I'd like to allow multiple selections in the dropdown menu, to create multiple tags in one go. Adding "multiple" doesn't work, however: it simply results in creating a tag for the current user, posting the Post.
Can anyone suggest a way I can use a single select field to create multiple tags?
A bit late to the party, but I solved this problem using the awesome jQuery Chosen plugin which makes multiple selects look really good.