I have the following in my js.erb file:
$('#menu').after($("<%=escape_javascript(render 'boards/customize', :board => #board, :templates => #templates, :types => #types)%>")
So I try to pass some locals to my partial
In my _customize.html.erb
<div id="customize">
<ul id="categories">
<% #types.each do |type|%>
<li><%=link_to type.name, change_type_board_path(board, :type_id => type.id), :remote => true %></li>
<% end %>
</ul>
<div id='carousel'>
<%=render 'boards/carousel', :templates => templates %>
</div>
</div>
I get the following error:
undefined local variable or methodboard' for #<#:0x00000103893e48`>
How are you supposed to pass in these variables to partials in Rails?
In Rails 3:
render :partial => 'boards/customize',
:locals => { :board => #board , :templates => #templates, :types => #types }
Related
Hello im doing simple update..
logged_customer_controller.rb
class LoggedCustomerController < ApplicationController
before_filter :authorize
helper_method :current_customer
layout "frontend"
def current_customer
#current_customer ||= Customer.find(session[:customer_id]) if session[:customer_id]
end
def authorize
if session[:auth] != true
redirect_to login_path, :notice => "Not logged."
end
end
def show
end
def edit
end
def update
respond_to do |format|
if current_customer.update_attributes(params[:current_customer])
format.html { redirect_to view_path, notice: 'Customer was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: current_customer.errors, status: :unprocessable_entity }
end
end
end
end
routes.rb
match "view" => "logged_customer#show", :via => :get
match "edit" => "logged_customer#edit", :via => :get
match "edit" => "logged_customer#edit", :via => :put
edit.html.erb
<%= form_for current_customer, :url => url_for(:controller => 'logged_customer', :action => 'edit'), :html => { :class => 'form-horizontal' } do |f| %>
<% if current_customer.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(current_customer.errors.count, "error") %>.
</div>
<ul>
<% current_customer.errors.full_messages.each do |msg| %>
<li> <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
...
I can display localhost:3000/view where is edit button. At localhost:3000/edit the form is displayed with autofilled informations, everything looks good. When i click on submit button im redirected to same edit autofilled form but WITHOUT any error ? So i guess there is some mistake because updating failed and another mistake that it dont render errors. What im doing wrong ?
I have logged_customer_controller.rb because customer_controller.rb is for administration purposes and is under authorization.
at Development.log i have only (looks good)
Started PUT "/edit" for 127.0.0.1 at 2013-08-19 14:54:30 +0200
Processing by LoggedCustomerController#edit as HTML
Parameters: {...}
<Executing SQL ...>
Rendered logged_customer/edit.html.erb within layouts/frontend (67.0ms)
Completed 200 OK in 89ms (Views: 33.0ms | ActiveRecord: 56.0ms)
Well, on your form_for you say that the action is the edit one, when it should be update.
<%= form_for current_customer,
:url => url_for(:controller => 'logged_customer', :action => 'edit'),
:html => { :class => 'form-horizontal' } do |f| %>
This way, when you submit it would hit the edit action. Change this and you are good to go.
Also, change you route as #Mattherick said:
match "update" => "logged_customer#update", :via => :put
Change your routes (rails 3):
match "view" => "logged_customer#show", :via => :get
match "edit" => "logged_customer#edit", :via => :get
match "update" => "logged_customer#update", :via => :put
Change your routes (rails 4):
get "view" => "logged_customer#show"
get "edit" => "logged_customer#edit"
patch "update" => "logged_customer#update"
Change your form:
<%= form_for current_customer, :url => url_for(:controller => 'logged_customer', :action => 'update'), :method => "patch", :html => { :class => 'form-horizontal' } do |f| %>
<%= # your form fields %>
<% end %>
Here is my form view code:
<div class="row">
<%= semantic_form_for #new_athlete_sport, :remote => true, :html => { :class => "new_sport", :"data-type" => 'json', :id => '' } do |f| %>
<%= f.label "Sport" %>
<%= f.select :sport_id, Sport.all.collect { |sp| [sp.name, sp.id] }, {}, { class: "chosen", id: "" } %>
<br />
<%= f.submit %>
<% end %>
</div>
For some reason the <form> tag isn't showing up in the DOM, but every other fields show up..
Seems the syntax in form_for has some problem.
Try remove "id" and move "data-type" from this
<%= semantic_form_for #new_athlete_sport, :remote => true,
:html => { :class => "new_sport", :"data-type" => 'json', :id => '' } do |f| %>
to this
<%= semantic_form_for #new_athlete_sport, :remote => true, :data=> {:type=> 'json'}, :html => { :class => "new_sport"} do |f| %>
I'm new to Rails and JQuery so I'll try to explain this as best I can. I'm trying to pass my JQuery datepicker values to my rails controller. The page has data on it that I want the user to be able to filter based on a date range. There are a number of different posts on this subject:
Passing the variables from from jquery to rails controller
How to pass variables from AJAX form into controller?
I have tried to follow the guidance of these posts, but I'm still having trouble.
Here is my partial _dateFilter.html.erb =>
<%= form_tag({:controller => 'events', :action => 'dateFilter', :class => 'date_form'}, :remote => true) do %>
<%= datepicker_input "event", :start_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
<%= datepicker_input "event", :end_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
<%= submit_tag "Submit" %>
<% end %>
Here is the generated HTML =>
<form accept-charset="UTF-8" action="/events/dateFilter?class=date_form" data-remote="true" method="post">
<input class="dateFilter" id="event_start_date" name="event[start_date]" size="30" type="text" /><script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){jQuery('#event_start_date').datepicker({"dateFormat":"mm/dd/y"})});
//]]>
</script>
<input class="dateFilter" id="event_end_date" name="event[end_date]" size="30" type="text" /><script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){jQuery('#event_end_date').datepicker({"dateFormat":"mm/dd/y"})});
//]]>
</script>
<input name="commit" type="submit" value="Submit" />
</form>
Here is the dateFilter.js.erb =>
$('.dateFilter').append('<%= escape_javascript(render(#events)) %>');
Here is the events_controller =>
def dateFilter
#events = Event.find(:all, :conditions => ["EVENT_DATE_TIME_LOCAL BETWEEN ? AND ?", params[:start_date], params[:end_date]])
respond_to do |format|
format.html
format.json { render json: #events }
format.js
end
end
Finally here is the application.js =>
$(document).ready(function() {
$('#date_form').submit(function (){
$.ajax({
type: 'POST',
url: "/events/dateFilter",
beforeSend: function (xhr) {xhr.setRequestHeader("Accept", "text/javascript");},
data: { 'start_date' : $("input[name='event[start_date]']").datepicker(), 'end_date' : $("input[name='event[end_date]']").datepicker()}
success: function(data) { <%= render "events/dateFilter" %>}
});
});
This is log data from the server =>
Started POST "/events/dateFilter?class=date_form" for 127.0.0.1 at 2012-12-12 15:09:39 -0500
Processing by EventsController#dateFilter as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"J/HU+DxNmIwEhu3keN071k6MGCCD/n/1UhXUD0MkH1Q=", "event"=>{"start_date"=>"12/06/12", "end_date"=>"12/13/12"}, "commit"=>"Submit", "class"=>"date_form"}
Event Load (1.7ms) SELECT "TBLEVENT".* FROM "TBLEVENT" WHERE (EVENT_DATE_TIME_LOCAL BETWEEN NULL AND NULL)
Rendered collection (0.0ms)
Rendered events/dateFilter.js.erb (0.3ms)
Completed 200 OK in 8ms (Views: 3.4ms | ActiveRecord: 1.7ms)
One of the problems is that I keep getting "NULL" for the values of data I am trying to pass. I'm not even sure that I am going about this correctly, any help/feedback on this would be greatly appreciated. Thanks.
From your log data, this is in the Parameters:
"event"=>{"start_date"=>"12/06/12", "end_date"=>"12/13/12"}
which means start_date and end_date are in a Hash called event, so your controller should look like this:
def dateFilter
eventIn = params[:event]
#events = Event.find(:all, :conditions => ["EVENT_DATE_TIME_LOCAL BETWEEN ? AND ?", eventIn[:start_date], eventIn[:end_date]])
respond_to do |format|
format.html
format.json { render json: #events }
format.js
end
end
One other small thing - the form_tag class should be set like so:
<%= form_tag({:controller => 'events', :action => 'dateFilter'}, :class => 'date_form', :remote => true) do %>
i started learn HAML: and i can't translate flash block to HAML:
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong><%= value %></strong>
</div>
<% end %>
Here you go:
= flash.each do |key, value|
.alert{ :class => "alert-#{key}" }
%button.close{ :data => { :dismiss => "alert" } } x
%strong
= value
Just FYI you can add attributes to any element by attaching them as a hash after the declaration. If you don't specify an element, just a class or ID, HAML makes that element a div with the given class or id. But you could do this many ways. For instance, these are all the same:
%div{:class => 'foo bar', :id => 'test' }
.foo{:class => 'bar', :id => 'test'}
#test.bar{:class => 'foo'}
#test.foo.bar
All output: <div class="foo bar" id="test"></div>
You need to put computed attributes in the hash though, i.e.:
- klass = "bar"
%div{ :class => klass }
Outputs: <div class="bar"></div>
Also, note that in all the examples above, the :attribute => 'value' can be expressed as attribute: 'value', e.g.:
%button.close{ data: { dismiss: 'alert' } } x
Hope that helps.
When the user is clicking on a link with the class "edit_resource", the content of a div should be replaced by a partial. Here is my code:
$(".edit_resource").click(function(){
var id = $(this).attr("id")
$('#id' + id + '_show').html("<%= escape_javascript(render :partial => 'form', :locals => {:#resource => resource})%>")
})
The code is working as expected, except that that escape_javascript doesn't work. The new content of the div is the text
<%= escape_javascript(render :partial => 'form', :locals => {:#resource => resource})%>
, and this is also what is shown on the page.
No code is executed, and my partial isn't rendered. I have tried to use <%== instead of <%= without luck.
I have also tried
<%= raw escape_javascript(render :partial => 'form', :locals => {:#resource => resource})%>
I have even tried to replace the partial part of the code with just simple rails code. That didn't help either.
What can I do?
I use Rails 3.0.10, and my javascript_include_tag is like this:
<%= javascript_include_tag 'jquery-1.6.2.min', 'jquery-ui-1.8.16.custom.min', 'application', 'jquery.rails.js'%>
Add .html_safe after the closing bracket of escape_javascript
I have put my js in a js.erb-file, and finally managed to use my variables in the right way.
Here's my code:
$('#id' + '<%= #id %>' + '_show').html('<%= escape_javascript(raw render :partial => 'form', :locals => {:#resource => Resource.find_by_id(#id)}).html_safe %>')
And in the controller:
format.js { #id = params[:id]}
In the view: <%= link_to t('edit'), resources_path(:id => resource.id), :remote => true %>
I don't think you can have :#resource # followed by an symbol :
<%= escape_javascript(render :partial => 'form', :locals => {:#resource => resource})%>
Just take that # out
<%= escape_javascript(render :partial => 'form', :locals => {:resource => resource})%>
and use the variable by calling
resource in your partial