#social_list
= form_tag notes_path, class: 'form clearfix', id: 'add_post_form',multipart: true, remote: true
.control-group
= text_area_tag "comment[text]", '', :placeholder => 'Post to the community', :cols => nil, :rows => nil, :class => 'mention expand-without-submit'
= file_field_tag "picture", id:'image_upload', accept: 'image/png,image/gif,image/jpeg'
= submit_tag "Post your message", class: 'btn btn-success btn-mini'
= form_tag images_path
= submit_tag 'asad'
= render "dashboards/activity_stream_filter"
I want to add second form but it is giving me this error.
syntax error, unexpected keyword_ensure, expecting $end
I guess some scoping issue is messed up or is the possible to have multiples form in single page ?
The form_tag method takes a block for the form contents. In your first form you have’t provided a block at all since nothing is indented below it. In the second case there is content indented below, but you haven’t included the do. This second case is causing the syntax error.
What I think you want is
#social_list
- # note 'do' added to this line:
= form_tag notes_path, class: 'form clearfix', id: 'add_post_form',multipart: true, remote: true do
- # this section indented:
.control-group
= text_area_tag "comment[text]", '', :placeholder => 'Post to the community', :cols => nil, :rows => nil, :class => 'mention expand-without-submit'
= file_field_tag "picture", id:'image_upload', accept: 'image/png,image/gif,image/jpeg'
= submit_tag "Post your message", class: 'btn btn-success btn-mini'
- # 'do'added to next line
= form_tag images_path do
= submit_tag 'asad'
= render "dashboards/activity_stream_filter"
Related
I'm having a little difficulty with the Tire and Elastic Search functionality.
I have a Listing that has a Property. I'm trying to get a basic search form working so that I can create a query from the Property.
# listings/index.html.erb
<%= form_tag searches_listings_path, method: :get do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= text_field_tag :property_postcode, params[:property_postcode] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
At the moment, whenever I try filtering down the search results, it seems like the property_postcode is being ignored and all results are being returned.
# Listing.rb
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :id, type: 'integer'
indexes :title, boost: 10
indexes :description, analyzer: 'snowball'
indexes :posted_at, type: 'date'
indexes :property do
indexes :postcode, :type => 'string'
end
end
def self.search(params)
tire.search(page: params[:page], per_page: 5, load: true) do
query do
boolean do
must { string params[:query], default_operator: "AND" } if params[:query].present?
must { term "property.postcode", params[:property_postcode] } if params[:property_postcode].present?
end
end
end
end
def to_indexed_json
to_json(:include => {
:property => {
:only => [:postcode]
}
})
end
And finally for the property
#Property.rb
class Property < ActiveRecord::Base
belongs_to :listing, touch: true
after_touch() { tire.update_index }
end
Then finally
rake environment tire:import CLASS=Listing FORCE=true
Thanks in advance,
Ryan
Property.search(page: params[:page], per_page: 5, load: true) do
query { string params[:query], default_operator => "AND" } if params[:query].present?
filter :term, "property.postcode" => params[:property_postcode] if params[:property_postcode].present?
end
be sure to turn on routing in your mapping too
mapping :_routing => { :required => true, :path => property.postcode } do
I have this form, which is not bound to any model, that I want to ajaxify. I've tried to figure out how to get it to submit via ajax, but I must be doing something wrong because it is not working (it just does a regular POST).
I can confirm that the form tag renders with a 'remote' attribute, but there is not js added anywhere to the form. I also added the :confirm just to see if that would work as well. It does not.
jquery and jquery_ujs are both loaded on the page.
%form{ :action => "/newsletter", :confirm => "Are you sure?", :remote => true, :method => "post", :id => "newsletterForm"}
%p
= label_tag(:q, "Subscribe to our newsletter:")
%p
= text_field_tag(:q, nil, :placeholder => "Your email address")
= button_to("Subscribe", :remote => true)
I just wrote this doing something similar with a form get:
= form_tag('/signup', :method => "get", :remote => true, :id=> 'signup-form') do
%label{:id => 'signup-label', :for=> 'signup-box'}
Enter your email address
= text_field_tag "signup-box", params[:signup], :class => 'text', :required => true, :id => 'signup-box'
= submit_tag "Sign Up", :id => 'signup'
Controller:
class SignupController < ApplicationController
def index
puts "***************************************************"
puts "email sign up"
puts "***************************************************"
render :nothing => true
end
end
I'm know this is a newbie question just not sure what I'm missing and decided to post hear after my usual search through Google. I'm trying to post content to the database from a form in the footer of the application (for a newsletter) the view is therefore repeated throughout the application. Right now when I submit the form a new object is created in the database but all the fields are "NULL". It seems I need to put the #newsletter variable somewhere, I'm just not sure where.
Partial I'm Rendering in the View
<%= form_tag({:controller => "newsletters", :action => "create"}, :method => "post", :id => "footer_email_form") do %>
<%= text_field_tag :first_name, '', id: "footer_email_firstname" %>
<%= text_field_tag :last_name, '', id: 'footer_email_lastname' %>
<%= text_field_tag :email, '', id: 'footer_email_address' %>
<%= submit_tag "Submit", :name => nil, id: 'footer_email_submit', class: "btn btn-primary" %>
<% end %>
Controller (Create Action)
class NewslettersController < ApplicationController
def create
#newsletter = Newsletter.new(params[:newsletter])
if #newsletter.save
format.html { redirect_to 'pages#home', notice: 'Thank You for signing up for our newsletter' }
format.json { render json: #newsletter, status: :created, location: #newsletter }
else
format.json { render json: #newsletter.errors, status: :unprocessable_entity }
end
end
end
Routes
resources :newsletters, :only => [:create, :destroy]
Use something like this:
<%= form_for Newsletter.new do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= f.text_field :email %>
<%= f.submit_tag "Submit", class: "btn btn-primary" %>
<% end %>
The way your original form works the params are submitted like this:
params = {
:first_name => 'A',
:last_name => 'B',
:email => 'C',
# and so on...
}
Now if you do #newsletter = Newsletter.new(params[:newsletter]) nothing will happen, because params[:newsletter] is nil and therefore all your attributes are going to be nil (and show up as NULL in the DB).
You should always have an eye on the development log. It's going to help you debug such errors.
In the part of code below I am still getting this error:
syntax error, unexpected keyword_ensure, expecting $end):
17: %input.btn.btn-primary{:name => "commit", :type => "submit", :value => "Set As Profile Picture"}/
Code:
#settings_photos_window.wide_width.modal.fade
= form_tag '/photos/set_avatar', :method => 'post' do
.modal-header
%a.close{"data-dismiss" => "modal"} ×
%h3 Choose Your Profile Picture
#choose_profile_pic.modal-body
= hidden_field_tag 'photo[avatar]', ((#cur_avatar) ? #cur_avatar.id : '')
- #settings_photos.each_slice(2) do |slice|
.row{:style => "text-align: left;"}
- slice.each do |photo|
- (photo.avatar == 1) ? (bg_color = 'background: #28AD4B;') : (bg_color = 'background: #fff;')
.span5.choose_picture{:style => "cursor: pointer; margin-right: 40px;"}
%div
= image_tag(photo.photo.url(:thumb), :class => 'thumbnail', :id => photo.id, :style => bg_color)
%br/
.modal-footer
%input.btn.btn-primary{:name => "commit", :type => "submit", :value => "Set As Profile Picture"}/
What could be wrong, or... doesn't exist any debugger for HAML?
Thanks
I think its probably the parentheses around the tenary statements. Its just a guess, but the rest of it seems perfectly acceptable to me.
I'm using a link_to to create an object in my Rails 3 app. Searching has given me the proper way to use link_to with the :post method, but I'm wondering if using link_to to pass in a name value for my object as well. Here is my link:
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :method => :post}, :class => "button white" %>
My profiles_controller.rb:
def create_inside
#todo = Insidetodo.new
#todo.save!
if #todo.save
redirect_to #profile.todo, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
My todo.rb model:
class Todo < ActiveRecord::Base
has_and_belongs_to_many :profiles
validates :name, :presence => true
end
Is there a way to add in :name => "#{#user.profile.todotext}" to the link_to so that it passes and saves? I don't know if it's creating properly because at the moment when I click a link_to I get a validation error - Validation failed: Name can't be blank.
For passing name in the link_to
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :name => "#{#user.profile.todotext}", :method => :post}, :class => "button white" %>
and the controller must be
def create_inside
#todo = Insidetodo.new(:name => params[:name])
if #todo.save
redirect_to #todo.goal, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
But the link_to will pass the name parameter in url only(like Todo text).
If you want the name not to be sent in the url, you might want to use a form and use a submit button instead of the link as it is representing an action and not a link.
<%= form_tag(:controller => "profile", :action => "create_profile") do -%>
<%= hidden_field_tag :name, #user.profile.todotext %>
<%= submit_tag "Todo Text" %>
<% end -%>