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
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 %>
I'm new to ROR but I face this problem:
in the index page I have
<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :method => :post %>
when user presses the "Yes button" the url that passed to the controller contains all the parameters explicitly to the user.
vote?id=1&user_answer=yes
in routes.rb I have:
match 'vote' => 'polls#vote', :via => :post
Any help is appreciated
edit: the entire index.html.erb
Polls
<% #polls.each do |poll| %>
<p>
<%= poll.question %>?
<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :method => :post %> (<%= poll.yes %>) /
<%= button_to 'No', { :action => 'vote', :id => poll.id, :user_answer => 'no' }, :method => :post %> (<%= poll.no %>)
</p>
<% end %>
The default method for button_to is post, so no need to explicitly specify it unless you want to use other verbs (get, put).
If you don't like the params appended to the url, you might want to use a form instead.
Or, just add the form hash to your button_to tag:
<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :form => {:data_type => <your choice (html, json)> %>
I am trying to format a form and the text fields respond to some methods, and not others.
I can do things like:
f.input :name, :input_html => { :maxlength => 10 }
f.input :name, :input_html => { :disabled => true }
But if I try to do any of the following, they do not work:
f.input :name, :input_html => { :size => 10 }
f.input :name, :input_html => { :class => 'autogrow' }
f.input :name, :input_html => { :rows => 10, :cols => 10 }
When I try using :size, for instance, the generated html shows that size=10, but is not reflected in the actual form.
These were more or less pulled right from the Formtastic documentation on Github, which the Activeadmin documentation refers to.
I am not sure if your question is solved or not.
However according to Formastic Official WIKI, your code should work:
Customize HTML attributes for any input using the :input_html option.
Typically this is used to disable the input, change the size of a text
field, change the rows in a textarea, or even to add a special class
to an input to attach special behavior like autogrow textareas:
<%= semantic_form_for #post do |f| %>
<%= f.inputs do %>
<%= f.input :title, :input_html => { :size => 10 } %>
<%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
<%= f.input :created_at, :input_html => { :disabled => true } %>
<%= f.input :updated_at, :input_html => { :readonly => true } %>
<% end %>
<%= f.actions %>
<% end %>
https://github.com/justinfrench/formtastic
if your code doesn't work , please check out the error logs, or put more debug info to your erb file, to see if you r rails is running under production mode.
i had the same problem. i wanted a nested form for edit with custom text field size.this worked for me.
form do |f|
f.inputs "Header" do
cf.input :name, :input_html => { :class => 'some_style', :rows => 2, :style => 'width:50%'}
end
f.actions
end
so basically u have to create your own class or just work with the :style.
For nested form u can use this code
form do |f|
f.inputs "Header" do
f.has_many :name,:allow_destroy => true,:new_record => true do |cf|
cf.input :first_name, :input_html => { :class => 'some_style', :rows => 2, :style => 'width:50%'}
end
end
f.actions
end
I have the following form_tag working:
<%= form_tag url_for(:controller => "profiles", :action => "remove_academic", :method => :delete), :id => "remove_major_goal", :remote => true do %>
However, the HTML produced shows that :method => "delete" isn't working. So I found a few answers here on form_tag and tried this:
<%= form_tag url_for({ :controller => "profiles", :action => "remove_academic", :method => "delete" }, { :id => "remove_major_goal", :remote => true }) do %>
However that kicks back an error. What am I doing wrong?
DELETE is not a valid value of the method attribute for a HTML form element. You would probably be better inserting a <input type="hidden" name="method" value="delete" /> inside the form (or use a helper method to do so).
Update:
Try one of these:
form_for url_for(:controller => "", :action => ""), :method => "delete", …
form_for { :controller => "", :action => "" }, { :method => "delete", … }
The second set of braces in the second form maybe unnecessary. Likewise, they might be needed in the first form.
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 }