How to use form_for of rails in other controller? - ruby-on-rails

I am currently writing a rails project. There are two model, course and post. What I want is to allow people to add comments(posts) in course's show page. I have a form_for for post, like this:
<%= form_for(#post) do |f| %>
<div class="well">
<h4>PICK a nickname:</h4>
<div class="form-group">
<%= f.text_field :user , class: "form-control", rows: "3"%>
</div>
<h4>Leave a Comment:</h4>
<div class="form-group">
<%= f.text_area :content , class: "form-control", rows: "3"%>
</div>
<%= f.submit "Add New Comment", class: "btn btn-primary" %>
</div>
<% end %>
How can I use this form_for in course's show html page?
Also, when submitting the post, I want the page to stay at the show page.

render as partial from course's show page (but don't forget put #post)
render partial '/comments/comment', locals: {post:post}
use remote true (with right method) / or redirect user back to
form_for post remote: true, method:'put/post/path'
for redirect back need to know your routes.rb

Related

Rails 4 ajax load partial into view

there is a way to put an input dynamically from button?
Example:
this is the form:
<%= form_for(#order) do |f| %>
.
...
<div class="field">
<%= f.label :productlist %><br>
<%= f.select :productlist, #products, :prompt => 'Select one!' %>
<%= link_to 'Add More', '#', id: 'products-link' %>
</div>
<div id="newproduct">
<p>load here the new input on Add more link</p>
</div>
<div class="field">
<%= f.label :status %><br>
<%= f.text_field :status %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
so i thinked up to ajax and a way to load a partial input into the form, but im not understanding the correct way to do this.
Thank you
In your javascript file add:
$("a#products-link").click(function(){
$.get('/partials/products_form', function(data){
$("div#newproduct").append(data);
}, 'html');
});
Essentially what is happening is that you are going to send a get request via ajax to a form in /partials/products_form or wherever you choose to keep your form partial. And then this html will be appended to whatever div/identifier you choose in this case div#newproduct.

Rails gives a InvalidAuthenticityToken error. If I reload the page it works

Here's how my application works : User uploads a photo > He gets redirected to a page with a list of photos > The user set titles and descriptions of photos and hit save. Now sometimes when a user hit save Rails give InvalidAuthenticityToken error. But if the user reload the page before he hit save, Rails won't give the error and everything will work fine.
Why is it happening like this? I use devise and omniauth.
The form :
<%= form_tag update_submits_photos_path, method: :put do %>
<% #submits.each do |submit| %>
<div class="cont">
<%= fields_for "submits[]", submit do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="row">
<div class='subDetails'>
<%= f.label :title, 'Title :', class: "updLbl" %>
<%= f.text_field :title, class: "updInp" %>
<%= f.label :description, 'Description :', class: "updLbl" %>
<%= f.text_field :description, class: "updInp" %>
</div>
</div>
<% end %>
</div>
<% end %>
<%= submit_tag "Update All",class: 'btn btn-primary btn-lg uploadedUp' %>
<br>
<% end %>
Update : I just updated to Rails 4.2 stable and now the problem have disappeared!
There are two paths you can follow in order to resolve the issue.
First is add following line:
skip_before_filter :verify_authenticity_token, only: :the_action
to the corresponding controller, where :the_action is most likely update.
Second option is to add the token to your form:
<%= hidden_field_tag :authenticity_token, form_authenticity_token -%>

Rails form validation failure for a form on a different models show view

I have a form much like the one in the Rails getting started guide where a comments form is displayed on the show view of a different model. I'm having trouble with rendering the show view if model validation fails because when the show view re-renders I lose the ID of the other model with a "missing required keys: [:id]" error. It also redirects to the wrong url, topics/11/posts instead of just topics/11.
How can I get this to work?
My render path for an invalid form in the controller is
format.html { render template: "topics/show" }
and the form is
<%= form_for([topic, topic.comments.build]) do |f| %>
<h2>Add new discussion topic</h2>
<p>
<%= f.text_field :title, class: "form-control" %>
</p>
<p>
<%= f.text_area :body, :rows => 6, class: "form-control" %>
</p>
<p>
<%= f.submit 'Submit', :class => 'btn' %>
</p>
<% end %>

how to render Multiple forms in one form in ruby on rails?

I have a static page and static controller. In the page am using Jquery UI Accordion in a four category. Each category has separate controller, model and view. I am trying to render all the category into the accordion and work like it does separately. I got a problem with that, So, I tried to create a form in the static page and do like the following. But all the forms opened in the first tab. I want to open and save the form separately as per the tabs. Any idea would be more appreciated. Thanks
<div class="funding_form">
<%= form_for(#funding_source, :remote => true) do |f| %>
<fieldset>
<legend>Funding Source</legend>
<div class="control-group">
<%= f.label :Source_name, 'Funding Source Name' %>
<%= f.text_field :Source_name %>
</div>
<div class="action">
<%= f.submit 'Save Source name', :class => 'funding_save', remote: true %> | <%= link_to 'Cancel', "#", {:remote => true, :class => 'funding_cancel_button' } %>
</div>
</fieldset>
<% end %>
</div>
<div class="packages_form">
<%= form_for(#package, :remote => true) do |f| %>
<fieldset>
<legend>Packages</legend>
<div class="control-group">
<%= f.label :Package_name, 'Package Name' %>
<%= f.text_field :Package_name %>
</div>
<div class="action">
<%= f.submit 'Save Packages', :class => 'package_save', remote: true %> | <%= link_to 'Cancel', "#", {:remote => true, :class => 'package_cancel_button' } %>
</div>
</fieldset>
<% end %>
</div>
Why don't you put an ajax request on the click action of the tab, to call an action in the controller. I.e. have a method in your #package controller which when called, will render the partial packages form. You can then call this with an ajax request :)
For example, in your controller you may want to render the partial "form" you can have a controller method like such:
def return_data
#package = Package.find(params[:id])
render partial "form"
end
and you could call it using an ajax request such as:
$.ajax({
url: '/your_path/return_data',
dataType: 'html',
success: function(data){
console.log(data)
},
error: function(data){
console.log(data)
});
you could then display it, perhaps assign the partial to a div and overwrite it every time you make an ajax request so one only ever displays :)

How submit data to a rails form via a URL?

I have a basic search form in my rails app that records each search that has been entered. I also want my users to be able to initiate a search with a url in the browser. E.g. http://www.example.com/searches?q=foo
I've played around with the different routing options and the logic in my controller but I can't seem to get this form:
<%= form_for(#search) do |f| %>
<div class="field">
<%= f.label :search %><br />
<%= f.text_field :search %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
To initiate a POST action on the database and submit a new search.
Thanks :)
Change your form to do a GET request instead of a POST then you can use both the form and the url:
<%= form_for(#search), :method => :get do |f| %>

Resources