call form rails with bootstrap twitter - ruby-on-rails

Someone has used to the modal of twitterboostrap calling to _form.html.erb ?
This is my try.
first I add the next line in the form (this is other form)
<%= simple_form_for(#terreno, html: {class: "form-horizontal"}, remote: :true) do |f| %>
<% if #terreno.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#terreno.errors.count, "error") %> prohibited this terreno from being saved:</h2>
<ul>
<% #terreno.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :titulo %>
<%= f.input :direccion %>
<%= f.input :region %>
<%= f.input :metrocuadrado %>
<%= f.input :precio %>
<%= f.input :votos %>
<%= link_to "Add image", new_image_path, id: "new_image", class: 'btn', remote: :true %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
The next step has been create the view for image. in this Case the view is view/images/new.js.erb
In this file I add the next code:
$('#new_image').hide().after('<%= j render("form") %>')
When I clicked the button "Add image" no show the form, but when I show the console(firebug), this show me the content of the _form.html.erb (remember this form belongs to image controller).
Any idea ?
Pdt: My english is very bad, sorry.

This may be down to HTML escaping of characters. Try:
$('#new_image').hide().after('<%= escape_javascript(raw render :partial => "form").html_safe %>');

Related

Ruby on Rails: Encountered a syntax error while rendering template:

Good night.
I'm doing the getting started guide of RoR, but I have a trouble:
When I click the New Article button that redirects to the form, Rails throw an 'Encountered a syntax error while rendering template.'
The new.html.erb code is
<h1>New Article</h1>
<%= form_with scope: :article, url: articles_path, local: true do |form| %>
<%= if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %>
prohibited this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<P>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
The code is the same as the guide's code, so i don't know where the error or errors can be.
I also attach the error screenshot if it's any help.
Rails error message when I want tho create a new Article
Here two problems:
wrong syntax in if condition (don't need to use =)
extra \n chars in your screenshot
Change your 5 line in app/view/articles/new.html.erb to:
<% if #article.errors.any? %>
<% %> executes Ruby code
<%= %> executes Ruby code and prints out
Also read about the difference on SO

How do I use select_year with form_for?

I have a form as follows:
<%= form_for(#car, :html => {:class => 'form-horizontal' }) do |f| %>
<% if #car.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#car.errors.count, "error") %> prohibited this car from being saved</h2>
<ul>
<% #car.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :model_year %><br />
<% %>
<% end %>
</div>
I want to have a collection select for the model year starting 100 years back and going to a year in the future. I have tried
<%= f.select_year :model_year%>
but it says that select_year is not a valid method. Tried
<%= f.date :model_year, :discard_month => true %>
also to no avail. Any thoughts?
select_year is a helper that generates a full select tag with options. Since you're using form_for instead of form_tag, you'll want to use a helper that can be called on a form builder object.
<%= f.select :model_year, (Time.zone.now.year - 100)..(Time.zone.now.year + 1) %>
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_year

Supply information automatically to simple form

I'm working on a simple little Rails social network and right now I am trying to automatically fill in the name of the currently signed in user when creating a new status update. My original code let them select their user name from a drop down which is silly - when you go on Facebook, you fill out the box and it automatically knows that it belongs to you. Here is my original code:
<%= simple_form_for(#status, html: {:class => "form-horizontal"}) do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this status from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id, collection: User.all, label_method: :full_name %>
<%= f.input :content %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
And here is what I am trying to replace it with:
<%= simple_form_for(#status, html: {:class => "form-horizontal"}) do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this status from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id, current_user.full_name %>
<%= f.input :content %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
It doesn't like this and says "can't convert Symbol into Integer"
How can I supply it with the logged in user's full name without ever giving them the option to chose another user?
user_id is the forreign key to User, it holds something like current_user.id not it's name.
It seems, that the user is already signed in, so you just need to show the name and provide the id to the create action:
<%= f.input :user_id, as: :hidden %>
<%= current_user.name %> <%# just for information %>
and set it in the new #status in the controller:
def new
#status = Status.new(user: current_user)
...
end
Your first input of the form is specified as an input for the :user_id which is an integer in your table of users. Putting a full name there is creating a mismatch in data types. Does this help?
Now that you want the username to be displayed without the user being able to change what is displayed, is there really a need to have this as an input? Can you just display the username in a normal <div></div>, so to speak?

rails Insert meta data from external url into table

I have a table for storing links. I'm trying to use the pismo gem for grabbing meta data from a url, and saving that to the table.
In my link controller I have a before filter :
def pismo_grab_meta_data
doc = Pismo::Document.new("http://google.com/")
#link_title = doc.title
end
Which gets called in the new and create action. In my view I pass the instance variable in a hidden field.
<%= f.hidden_field :name, :value => #link_title %>
This works when hard code the url as a string (like the above google example), but fails when I pass it the url params, like so:
doc = Pismo::Document.new(params[:url])
Error: undefined method `gsub!' for nil:NilClass
Pretty new to rails, so I assume I'm doing something stupid here. Any help is very welcome!
<%= form_for(#link, :html => { "data-behavior" => "submit-form"}) do |f| %>
<% if #link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#link.errors.count, "error") %>
prohibited this link from being saved:
</h2>
<ul>
<% #link.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :name, :value => #link_title %>
<div class="field urlsaver">
<%= f.label :url %><br />
<% if params[:link].present? %>
<%= f.text_field :url, :value => params[:link] %>
<% else %>
<%= f.text_field :url %>
<% end %>
</div>
<div class="actions">
<%= f.submit 'Save', :class => 'btn' %>
</div>
<% end %>
The value you are looking for should be in params[:link][:url].
Change the line to :
doc = Pismo::Document.new(params[:link][:url])

Rails Screencast - Completely stuck on "fields_for"

i am following this simple tutorial and i followed all the steps... but the browser simply doesn't show me anything i put inside the fields_for tag.
<%= form_for :activity, :url => activities_path do |f| %>
<% if #activity.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#activity.errors.count, "error") %> prohibited this activity from being saved:</h2>
<ul>
<% #activity.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Foto" %><br />
<%= f.text_field :photo %>
</div>
<div class="field">
<%= f.label "Foto per la home" %><br />
<%= f.text_field :photoHome %>
</div>
<% for info in #activity.infos %>
This is rendered<br>
<% fields_for "activity[info_attributes][]", info do |info_form| %>
This is not rendered<br>
<p>
Titolo: <%= info_form.text_field :title %>
</p>
<p>
Descrizione: <%= info_form.text_field :description %>
</p>
<% end %>
<% end %>
<p><%= submit_tag "Create activity" %></p>
<% end %>
The above code result is this:
What am i missing?
Rails is sometimes (well ok, often) a bit confusing in which helpers want a <% vs which helpers want <%=. Try
<%= fields_for "activity[info_attributes][]", info do |info_form| %>
The tutorial you're following doesn't, but that's from 2007, and in Rails 3 this was changed. Current documentation on fields_for is here.

Resources