I've been working on a ruby on rails app and trying to integrate the full calender gem but am experiencing errors. I can get the calendar to display but not the module to display to enter new events, every time I fix an error I seem to run into another.
I'm currently getting an ActionView::Template::Error
In my console
Started GET "/entries/new?_=1492426544714" for ::1 at 2017-04-17 11:55:52 +0100
Processing by EntriesController#new as JS
Parameters: {"_"=>"1492426544714"}
Rendered entries/_form.html.erb (46.9ms)
Rendered entries/_new.html.erb (78.2ms)
Rendered entries/new.js.erb (109.6ms)
Completed 500 Internal Server Error in 141ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (undefined method `entries_index_path' for #<#<Class:0x61746a8>:0x9f321c0>):
1: <%= simple_form_for #entry, remote: true do |f| %>
2: <div class="form-inputs">
3: <%= f.input :title %>
4: <%= f.input :date_range, input_html: { class: "form-control input-sm date-range-picker" } %>
app/views/entries/_form.html.erb:1:in `_app_views_entries__form_html_erb___640013074_85163292'
app/views/entries/_new.html.erb:9:in `_app_views_entries__new_html_erb__917401192_85193628'
app/views/entries/new.js.erb:1:in `_app_views_entries_new_js_erb__613427356_85255440'
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (78.1ms)
new.js.erb
$('#remote_container').html('<%= j render "new" %>');
$('#new_entry').modal('show');
_new.html.erb
<div class="modal fade" id="new_entry">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Create New Event</h4>
</div>
<div class="modal-body">
<%= render 'form', entry: #entry %>
</div>
</div>
</div>
</div>
any ideas?
It is a Rails magic, when you don't specify second option(url) for simple_form_for, Rails try to set it, in your case <%= simple_form_for #entry do |f|%> converts by Rails to <%= simple_form_for #entry, url: entries_index_path do |f|%>. In your routes, there is no such _path helper(as error says).
To resolve this issue run bundle rake routes and set the right url option.
Check the documentation
Related
It looks like my view should be updating. From the rails console it looks successful but my view does not update
I have this in my view
#views/pos_systems/sales.html.erb
<%= turbo_stream_from #sale %>
<%= turbo_stream.update dom_id(#sale), partial: "sales/sale" %>
<div id:"<%= dom_id (#sale) %>">
<%= render 'sales/sale', sale: #sale %>
</div>
the partial is in
#views/sales/_sale.html.erb
<h2><i class='far fa-snowflake fa-spin'></i> ...<%= #sale.comments %> </h2>
This what my console has when I do a broadcast_update, this will actually have the correct output but it does not change in my view
[132] pry(main)> s.broadcast_update
Rendered sales/_sale.html.erb (Duration: 0.1ms | Allocations: 8)
[ActionCable] Broadcasting to Z2lkOi8vanVtcHN0YXJ0LWFwcC9TYWxlLzMzOQ: "<turbo-stream action=\"update\" target=\"sale_339\"><template> <h2><i class='far fa-snowflake fa-spin'></i> ...Processing </h2> \n\n</template></turbo-stream>"
On my server I can see that it updated but the view never did
[ActionCable] [User 44] Turbo::StreamsChannel transmitting "<turbo-stream action=\"update\" target=\"sale_339\"><template> <h2><i class='far fa-snowflake fa-spin'></i> ...Processing </h2> \n\n</template></turbo-stream>" (via streamed from Z2lkOi8vanVtcHN0YXJ0LWFwcC9TYWxlLzMzOQ)
=> 1
My users can add in inventory and then print all the inventory out into lables using a pdf. This part works, now I need to create a form to get the range for items to be printed. The params come over and the all my variables are correct when I check them with byebug.
First issue is that my form wants to post instead of get
No route matches [POST] "/excute_print_tags.pdf"
Now if I add the Route the code runs and no output and I get this in the command line
Inventory Load (0.4ms) SELECT "inventories".* FROM "inventories" WHERE "inventories"."user_id" = $1 AND "inventories"."print_ref" >= $2 AND "inventories"."print_ref" < $3 [["user_id", 1], ["print_ref", 4], ["print_ref", 7]]
↳ app/pdfs/inventory_landscape_pdf.rb:18:in `inventory_number'
Rendering text template
Rendered text template (Duration: 0.0ms | Allocations: 2)
Sent data Westcoast Board and Ski_myskiswapinvenotry2020-08-05 14:51:40 -0700.pdf (0.9ms)
Completed 200 OK in 65227ms (Views: 0.6ms | ActiveRecord: 8.6ms | Allocations: 220205)
form code inside views/inventories/print_tags.html.erb
<%= form_with url: excute_print_tags_path(format: "pdf") do |form| %>
<div class="flex flex-wrap -mx-3 mb-2">
<div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
<div class="form-group">
<%= form.label :print_ref1 %>
<%= form.number_field :print_ref1, class: "form-control" %>
</div>
</div>
<div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
<div class="form-group">
<%= form.label :print_ref2 %>
<%= form.number_field :print_ref2, class: "form-control" %>
</div>
</div>
<div class="w-full md:w-1/3 px-3 md:mb-0 mt-8 ">
<div class="form-group">
<%= form.button class: "btn btn-primary" %>
</div>
</div>
</div>
<% end %>
Then inside the controllers/inventories_contoller
def excute_print_tags
#print_ref1 = params[:print_ref1]
#print_ref2 = params[:print_ref2]
#filtered_inventory = #user_inventory.where(print_ref: #print_ref1...#print_ref2)
byebug
respond_to do |format|
format.html
format.pdf do
pdf = InventoryLandscapePdf.new(#filtered_inventory)
send_data pdf.render, filename: "#{current_user.company}_myskiswapinvenotry#{Time.new}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
I have also tried adding the above code to print_tags section of the inventories and calling to itself. Same problem ... I think it has something to do with the way my forms is and it is only allowing it to be sent as a post instead of a get, technically its just a search right? I did try a search for that I found but that didnt solve this issue either. Any help is appreciated.
I had to change this line
<%= form_with url: excute_print_tags_path(format: "pdf") do |form| %>
To this line
<%= form_with url: excute_print_tags_path(format: "pdf"), local: true, method: :get do |form| %>
Thanks Les for your help
I'm new to rails and experimenting. After I managed to do this, I was now trying to do the following:
when clicking on a link for a setting's name, it's description should be rendered in a partial on the same page. I managed to render the partial, but only if I put some static text in it. For some reason I'm not able to pass the #fbs variable and thus to select the descriptions. I tried many different suggestions on other similar questions but I really don't get how I should do it. I'm using ruby 2.4.0 and rails 5.1
Here is my _settingsfb.html.erb (which is a partial of index.html.erb and which contains the Link_to)
<div class="container fluid">
<div class="row">
<!-- settings links -->
<div class="col-md-4 ">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Settings</h3>
</div>
<div class="panel-body">
<% #fbs.each do |fbs| %>
<ul>
<li> <%= link_to fbs.name, controller: 'settingsfb', action: 'show', id: fbs.id,remote: true %></li>
</ul><% end %>
</div>
</div>
</div>
<!-- setting description -->
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Description</h3>
</div>
<div class="panel-body">
<div id="desc">
<!-- here goes the description partial -->
</div>
</div>
</div>
</div>
here the settingsfb_controller (the relevant part)
class SettingsfbController < ApplicationController
before_action :set_setting, only: [:show, :edit, :update, :destroy]
# GET /settings
# GET /settings.json
def index
#settings = Setting.all
#fbs = Setting.where("Sns = 1")
end
# GET /settings/1
# GET /settings/1.json
def show
respond_to do |format|
format.js {render layout: false}
end
end
the show.js.erb
$("#desc").html("<%= j render 'settingsfb/show' %>");
and the _show partial
<%= #fbs.name %>
this is what I get when I click on a the link
Started GET "/settingsfb/show?id=1" for 127.0.0.1 at 2017-05-14 17:08:34 +0200
Processing by SettingsfbController#show as JS
Parameters: {"id"=>"1"}
Setting Load (0.3ms) SELECT "settings".* FROM "settings" WHERE "settings"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering settingsfb/show.js.erb
Rendered settingsfb/_show.html.erb (2.6ms)
Rendered settingsfb/show.js.erb (3.8ms)
Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.3ms)
ActionView::Template::Error (undefined method `name' for nil:NilClass):
1: <%= #fbs.name %>
app/views/settingsfb/_show.html.erb:1:in `_app_views_settingsfb__show_html_erb___617047289144576595_70310163068960'
app/views/settingsfb/show.js.erb:1:in `_app_views_settingsfb_show_js_erb___3755597588635490219_70310163079560'
app/controllers/settingsfb_controller.rb:17:in `block (2 levels) in show'
app/controllers/settingsfb_controller.rb:16:in `show'
any Ideas?
You should initialize #fbs before rendering after show action. So add #fbs = Setting.find(params[:id]) at the beginning of the show method.
The error indicates there is no #fbs variable. You're calling the settingsFbShow partial from the show.js.erb view but not passing any variables. Try
$("#desc").html("<%= j render 'settingsfb/show', locals: {fbs: fbs} %>");
Then in create.js.erb
<%= fbs.name %>
I will try my best to be clear, I myself am confused as to what is happening or how to explain it. I am building a Rails app.
I have two forms so far, both in modals, which are in partials.
I have my app rooted to a path called home, which is defined in application controller. In my html.erb, I want a particular styling on that page, so I have set it so that if I am on the home page, the body tag gets a class of 'homepage'. Here is the code from application_controller.rb:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
include SessionsHelper
def home
#home_page = true
# #user = User.new
end
end
As you can see, I have a code #user = User.new commented out. I added that because without it, Rails throws an error First argument in form cannot contain nil or be empty in reference to my signup form which in in a module, which is in a partial. Here is that form:
<div class="modal fade" id="loginModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h2 class="modal-title">Welcome Back!</h2>
</div>
<div class="modal-body">
<%= form_for(:session, url: sessions_path) do |f| %>
<%= label_tag :username %>:
<%= f.text_field :user_name, class: 'form-control'%>
<br>
<%= label_tag :password%>:
<%= f.password_field :password, class: 'form-control'%>
<br><br>
<%= submit_tag "Login", class: 'login-button'%>
<% end %>
</div> <!-- modal body -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div> <!-- modal-content -->
</div> <!-- modal-dialog -->
</div> <!-- login-modal -->
If uncomment that line in the application controller, I can go to the home page without errors. But then it messes up sessions, because from what I can tell, I have some new user waiting to be created, as that line is invoking the form I suppose. In addition, from terminal, I can see it is also finding user 1 for some reason. Here is part of the terminal output if it helps:
Started GET "/" for ::1 at 2015-08-19 23:35:30 -0400
ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ApplicationController#home as HTML
Rendered application/home.html.erb within layouts/application (2.2ms)
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Rendered users/_log_in_form.html.erb (14.2ms)
Rendered users/_new.html.erb (6.2ms)
Completed 500 Internal Server Error in 583ms (ActiveRecord: 6.2ms)
ArgumentError - First argument in form cannot contain nil or be empty:
actionview (4.2.3) lib/action_view/helpers/form_helper.rb:432:in `form_for'
app/views/users/_new.html.erb:15:in `_app_views_users__new_html_erb___2821927674300231403_70140243283980'
actionview (4.2.3) lib/action_view/template.rb:145:in `block in render'
so when I do log in, despite my user id being 1 in the database, it is being shown in terminal as 2 - which throws a bunch of undefined errors etc when I try to log my username or session[:user_id] in console(because ther IS no user 2). Of course, putting that #user = User.new line is wrong and I know this, but without it I get an error on the form as soon as I go to the home page, it seems to need to have the #user available...but with it there I get errors in sessions and cant log in or out properly...what is going on here? Here is the part of my application.html.erb that is rendering the forms, in case it helps. Also note that when I log in, I get no errors and the logout link appears, but I am unable to log out (more errors) and as I said, terminal shows me as having a id of 2 when it should be 1. I have the modal's render commented out for testing.
<% if logged_in? %>
<h1>Welcome back <%= current_user.user_name %>!</h1>
<%= link_to "Logout", sessions_path, method: :destroy %>
<% end %>
<ul>
<li> Find a Project</li>
<li lass="btn btn-info btn-lg" data-toggle="modal" data-target="#registerModal">Register</li>
<li lass="btn btn-info btn-lg" data-toggle="modal" data-target="#loginModal">Login</li>
</ul>
<div id='login-container'>
<!-- <%= render 'users/log_in_form' %> -->
</div>
<div class="signup-container">
<!-- <%= render 'users/new' %> -->
</div>
In your form do something like this:
<%= form_for(#user, url: :sessions_path) do |f| %>
and in yourApplicationController
def home
#home_page = true
#user = User.new
end
That is uncomment #user = User.new
See I will explain what is wrong with what you are doing. You didn't define the model object for the form_for method. The form_for method accepts 2 arguments and the 1st argument should be the model object like #user or #post etc. Please read more [here], (http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for), and you will completely understand how form_for is exactly used.
I am making a rails application. After a user has registered (I have
already created user registration with devise), they can fill out this
form that will contain their profile information. The form should
submit a post request to the create action in the informations controller, but for some reason I can't configure the routes properly. When i run rake routes, for informations#create, which is what the form should be going to, it has a blank path. There is also informations#index, which is what I guess its going to now. How do I get the form to go to informations#create if the path is blank? I have done this several times, and i can't find what is wrong. Here is the model:
class Information < ActiveRecord::Base
belongs_to :user
end
Here is the controller:
class InformationsController < ApplicationController
def new
#information = Information.new
end
def create
#information = Information.create(params[:information])
redirect_to student_path
end
def index
end
end
And here is the view for the new action.
<div class="span6 offset3 text-center">
<h1>Edit your information</h1>
<%= simple_form_for #information do |f| %>
<%= f.input :skills %>
<%= f.input :looking_for, :label => 'What help do you need?' %>
<%= f.input :my_idea %>
<%= submit_tag "Save", :class => "btn btn-primary btn-large" %>
<% end %>
</div>
Here is the line in the routes file:
resources :informations
I get the following error:
undefined method `information_index_path' for #<#:0x007f9c00c7b3e0>
Here is rake routes for the files
informations GET /informations(.:format) informations#index
POST /informations(.:format) informations#create
Here is my full stacktrace when trying to load the page that shows the form:
Started GET "/informations/new" for 127.0.0.1 at 2013-10-21 17:25:09 -0400
Processing by InformationsController#new as HTML
Rendered informations/new.html.erb within layouts/application (64.2ms)
Completed 500 Internal Server Error in 70ms
ActionView::Template::Error (undefined method `information_index_path' for #<#<Class:0x007ff03e8b34a0>:0x007ff03e8b23e8>):
2: <div class="span6 offset3 text-center">
3: <h1>Edit your information</h1>
4:
5: <% simple_form_for #information do |f| %>
6: <%= f.input :skills %>
7:
8:
app/views/informations/new.html.erb:5:in `_app_views_informations_new_html_erb__3172218935119285240_70334909089600'
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.7ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.1ms)
Started GET "/informations/new" for 127.0.0.1 at 2013-10-21 17:25:09 -0400
Processing by InformationsController#new as HTML
Rendered informations/new.html.erb within layouts/application (8.3ms)
Completed 500 Internal Server Error in 13ms
ActionView::Template::Error (undefined method `information_index_path' for #<#<Class:0x007ff03e8b34a0>:0x007ff03e8708a8>):
2: <div class="span6 offset3 text-center">
3: <h1>Edit your information</h1>
4:
5: <% simple_form_for #information do |f| %>
6: <%= f.input :skills %>
7:
8:
app/views/informations/new.html.erb:5:in `_app_views_informations_new_html_erb__3172218935119285240_70334908978600'
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (14.0ms)
Your problem is related to the fact that the word information is both the plural and the singular form. Just like water, or paper, or evidence. Rails considers those words uncountable.
There are two things you can do: either name your model something else or edit the file config/initializers/inflections.rb like so:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.uncountable %w( information )
end
Your form should be for one single information object. Not for a collection of information objects. Note that i have changed simple_form_for #informations to simple_form_for #information
<div class="span6 offset3 text-center">
<h1>Edit your information</h1>
<% simple_form_for #information do |f| %>
<%= f.input :skills %>
<%= f.input :looking_for, :label => 'What help do you need?' %>
<%= f.input :my_idea %>
<%= submit_tag "Save", :class => "btn btn-primary btn-large" %>
<% end %>
</div>
Also your failure is not because rails didn't generate information_index_path. Its because the named route for information#index is informations_path. the path is generated wrongly because you are using collection of objects.
Not sure if this will help, but try adding a = in front of simple_form_for. so that it will have a <%= instead of <%