I've recently updated one Rails 2.x applications to Rails 3 (3.0.20 at the moment, but the ultimate goal is 3.2.x). I noticed that one remote form stopped working properly. It sends requests and new records are created, but to tell that a full page reload is required.
There is absolutely nothing special in its definition:
form_for(#comment, :remote => true, :html => html_options) do |f|
<%= f.label :username, 'Name/nickname' %>
<div class="text">
<%= f.text_field :username, :maxlength => '60' %>
</div>
<%= f.label :email, 'E-mail' %>
<div class="text">
<%= f.text_field :email, :maxlength => '120' %>
</div>
<%= f.label :content, 'Content' %>
<div class="textinput-longer">
<%= f.text_area :content %>
</div>
<%= f.submit 'Add Comment', :value => 'Add comment' %>
<% end %>
There is also an event bound to the form
$('form#comment').bind({
submit: function() {
// disable inputs and change CSS
},
ajaxComplete: function(event, response, request) {
// insert comment
new Comment(response, this);
}
});
I suppose that some script specific for Rails 2.x might be missing?
try this:
remote_form_for(#comment, :html => html_options) do |f|
or
form_for(#comment,{ :remote => true, :html => html_options}) do |f|
I'm trying to add Bootstrap Markdown to a Rails app, using a gem. I am also trying to use simple_form to format the layout.
https://github.com/dannytatom/rails-bootstrap-markdown
Bootstrap Markdown requires a data-provide attribute, as shown in the html code from the project's github site: http://toopay.github.io/bootstrap-markdown/
<textarea name="content" data-provide="markdown" rows="10"></textarea>
But when I try to do this in my simple_form, I get an error: wrong number of arguments (0 for 1..2)
<%= simple_form_for(#essay) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :class %>
<%= f.input :title %>
<%= f.input :essay, :input_html => {:rows => 5, :placeholder => "Enter some text.", :class => "span6", :data-provide => "markdown" }%>
<%= f.input :status %>
</div>
Ruby doesn't accept a '-' in a symbol... You need to provide your data-provide as a string... Everything else looks okay. e.g.
<%= f.input :essay, :input_html => {:rows => 5, :placeholder => "Enter some text.", :class => "span6", "data-provide" => "markdown" }%>
Or, optionally I think you could also use the Rails data hash like so:
<%= f.input :essay, :input_html => {:rows => 5, :placeholder => "Enter some text.", :class => "span6", :data => {:provide => "markdown"} }%>
The latter method might be better if you're providing a lot of data attributes, but for a single attr the first seems more readable.
Is there a posibility to use i18n-translation outside a input-field ?
I want to group my input-fields with h6-headers like:
<%= simple_form_for #table, :html => { :class => 'form-horizontal' } do |f| %>
<hr class="rw_section_top">
<h6><%= t('.header_1') %></h6>
<hr class="rw_section_bottom">
<%= f.input :name %>
The problem is, that every translation I stored in de.yml doesn't work in the _form.html.erb. When I store it in the simple_form.de.yml it doesn't work too. Only the f.input translations are working.
I asked a question similar to this one about a week ago, but this is a slightly different perspective on it. The nature of the question involves being redirected to the correct controller.
I have a single resource, posts, and I have 4 different categories these posts can be under. I want each of these categories to be particular to a single controllers, and so I have the following in my routes.rb:
resources "code", :controller => :code_posts, :as => :code
resources "sports", :controller => :sports_posts, :as => :sports
resources "gaming", :controller => :game_posts, :as => :gaming
resources "the-nation", :controller => :personal_posts, :as => :the_nation
So now I can access posts through URLs like, for example, /code/1, /sports/34 to access the same post resource, but with each controller focusing on a single scope, namely a particular category.
This is all well and good, but my issue comes up when I try to edit or save particular posts. I have the following partial _form.html.erb (rendered in the new and edit views) in all the view folders for their particular controller:
<%= form_for #post do |f| %>
<div class="field">
<%= f.label :author %><br/>
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :title %><br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :category %>
<%= f.select :category_id, Category.all.collect {|c| [c.name, c.id] }, {:include_blank => true} %>
</div>
<div class="field">
<%= f.label :summary %><br/>
<%= f.text_area :summary, :rows => 5 %>
</div>
<div class="field">
<%= f.label :body %><br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :tag_tokens %><br/>
<%= f.text_field :tag_tokens, "data-pre" => #post.tags.map(&:attributes).to_json %>
</div>
<div class="field">
<%= f.submit "Submit" %>
</div>
<% end %>
So whenever I create or update a post, through whichever controllers, I always get redirected back to /posts/4, /posts/123, /posts/:id, whatever. I want to get redirected to the particular controller the post being edited or created lives under. So if I go to /code/new, and submit the new post, I want to be redirected to /code/1234, and not /posts/1234. How can I do this? For some reason I'm just having major mental mind blocks this morning. Thanks.
EDIT Updated <%= form_for #post do |f| %> to <%= form_for #post, :url => code_url(#post) do |f| %> and it works for /code/1/edit but not /code/new. When trying to access a new post form, I get the following error:
No route matches {:action=>"show", :controller=>"code_posts", :id=>#<Post id: nil, author: "Les Peabody", summary: nil, body: nil, created_at: nil, updated_at: nil, title: nil, category_id: 1, slug: nil>}
This is my CodePostsController#new method
def new
#post = Post.new(:category => Category.find_by_name("Programming"), :author => current_user.full_name)
end
You may specify the url in the form
<%= form_for #post, :url => gaming_path do |f| %>
You may use inheritance on the model.
The path in you form is determined by the class name, and in this case it is post.
If they mach with resources naming it should generate proper paths as well.
The dirty hack may be keeping objects path in it, I saw someone do that, but I do not recommend it too much.
I think the reason is the form_for method which takes for the update action as default the name of the parameter (here post) it gets.
So to change that, you have to add at the beginning (for the example resource code) the following:
<%= form_for #post, :url => code_path(#post) do |f| %>
This is of course only the URL for an existing object, the URL for a new object should be different. It should be there new_code_path (and no argument). So your partial should only contain the fields and labels, not the form_for call, because the URL should be different then.
You should look at the output of the call in the shell: bundle exec rake routes and search for the correct paths in the output.
So, ultimately what is important is how the form gets turned into HTML. If you look at the differences between a form that is meant for editing, and a form that is meant for a new object, there is only one thing that is ever really different that matters - the action URL.
In the case of a new form, the form tag should look something like:
<form accept-charset="UTF-8" action="/code" class="new_post" id="new_post" method="post">
and in the case of an edit form:
<form accept-charset="UTF-8" action="/code/1" class="edit_post" id="edit_post_1" method="post">
The only thing that matters to rails however is the name of the input elements (which are constant in both forms) and the action attribute in the form tag. That tells Rails whether or not it's rendering the edit or create action.
Since we're splitting up control of a single resource through multiple controllers, the standard form_for #post will not suffice since Rails can no longer automate the rendering process through convention (as we're doing a very unconventional thing). It is necessary to do some manual labor. The following will do the trick.
Convert the partial to the following:
<%= form_for #post, :url => path do |f| %>
<div class="field">
<%= f.label :author %><br/>
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :title %><br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :category %>
<%= f.select :category_id, Category.all.collect {|c| [c.name, c.id] }, {:include_blank => true} %>
</div>
<div class="field">
<%= f.label :summary %><br/>
<%= f.text_area :summary, :rows => 5 %>
</div>
<div class="field">
<%= f.label :body %><br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :tag_tokens %><br/>
<%= f.text_field :tag_tokens, "data-pre" => #post.tags.map(&:attributes).to_json %>
</div>
<div class="field">
<%= f.submit "Submit" %>
</div>
<% end %>
The path variable in there is a variable passed in through the :locals mechanism in the partial render, like so:
new.html.erb
<%= render :partial => "form", :locals => {:path => code_index_path} %>
and edit.html.erb
<%= render :partial => "form", :locals => {:path => code_path(#post)} %>
The nice thing with this solution is you can DRY up the code too by placing _form.html.erb in app/views/layouts or app/views/posts and reuse it in all of the new and edit views for all controllers that manipulate the Post resource in a consistent fashion. So rather than having:
<%= render :partial => "form", :locals => {:path => code_path(#post)} %>
we have:
<%= render :partial => "layouts/form", :locals => {:path => code_path(#post)} %>
I'm starting to use simple_form for a rails application, and while converting some of my forms, I came across one that has two models that it is working with, sort of an embedded form. Is this possible with simple_form?
<% simple_form_for :topic, :url => forum_topics_path do |t| %>
<%= t.input :name, :label => 'Topic' %></p>
<p>First Post:<br/></p>
Title: <%= text_field :post, :title %> <--- this is where i start having problems
Body: <%= text_area :post, :body %>
<%= t.submit 'Save' %>
Thanks
Use simple_fields_for :
<%= simple_form_for :topic, :url => forum_topics_path do |topic_builder| %>
<%= topic_builder.input :name, :label => 'Topic' %>
<%= topic_builder.simple_fields_for :post do |post_builder| %>
<p>First Post:</p>
<%= post_builder.input :title, :input_html => { :size => 30 } %>
<%= post_builder.input :body, :as => :text, :input_html => { :rows => 20, :cols => 50, :class => 'resizable' } %>
<% end %>
<%= topic_builder.submit 'Save' %>
<% end %>
Notes
Note the = symbol in <%= simple_form_for ... and <%= simple_fields_for (required in Rails 3.x)
Removed "Title:" and "Body:" text. Use the label generated for the inputs and style their location with CSS as needed.
Added example of using input_html
There's another approach that I'm using and it works great. Ryan Bates (RailsCasts) has created a gem to handle this.
See https://github.com/reu/simple_nested_form for the details.