Re: first app - some questions
Thank you for the detailed description! I have one question about the @state. Where does the method user_id come from? I've been going through the unabridged source and Camping::Session reference, but I guess I'm not proficient enough:) And what else can be also saved in the @state? regards seba 2014-06-07 18:42 GMT+02:00 Magnus Holm judo...@gmail.com: On Saturday, May 3, 2014, Sebastjan Hribar sebastjan.hri...@gmail.com wrote: Hi guys, just a little feedback from a beginner. I've finished my first camping app. Thank you for your help and guidance. The finished app is about quality management and enables users to fill out forms. Based on those forms different quality parameters are recorded per user and per project. These can be called up in summary reports for desired time frame and user. Along with all the forms for a period the average quality evaluation is also displayed. Reports can only be generated by quality managers. I've managed to reuse only one html form for creating, viewing and editing. In addition, I've setup the form in such a way, that submitting is possible only when the form was called via »new« or »edit« route and the user has appropriate authorizations. I have one question regarding the session. I don't actually know how to leverage or use it. According to the reference the session adds states to the app. Can someone explain a bit more? Sorry for the late reply. Here's an example of state: module App::Controllers class Index def get if @state.user_id Welcome user number #{@state.user_id} else render :login end end class Login def post # Just log everyone in. No passwords here! @state.user_id = @input.user_id redirect Index end end end module App::Views def login form action: R(Login), method: :post do label do p User ID: input name: :user_id end button Log in end end end Here's an example of the flow when the user visits the site: 1. User visits GET /. Index#get is called. 2. The state is empty, so the #login view is rendered 3. The user enters his user ID (let's say 5). 4. When the user presses Log in, the browser will do a POST /login with user_id=5 as parameters 5. Login#post is invoked. This sets the state variable and then redirects to /. 6. The user's browser then shows GET /. Now he gets a welcome message, not the login form. Notice how the same request is done in both step 1 and 6 (GET /), but the second time the user gets a completely different page. That's because the state is different. You might know about cookies: sessions are like cookies that only the server know how to set. It's impossible for the client to set its own session; every session has to be set through the @state-variable in an action. I hope this clarifies state/sessions a bit. Don't hesitate to ask more if you're confused. // Magnus -- // Magnus Holm ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list
Re: first app - some questions
On Monday, June 9, 2014, Sebastjan Hribar sebastjan.hri...@gmail.com wrote: Thank you for the detailed description! I have one question about the @state. Where does the method user_id come from? I've been going through the unabridged source and Camping::Session reference, but I guess I'm not proficient enough:) And what else can be also saved in the @state? regards seba @state (and @input) is an instance of Camping::H. Camping::H is a Hash with some sugar. You can use it like a regular Hash (it's actually a subclass of Hash): @state[user_id] = 1 @state[user_id] # = 1 But it also supports method-like access: @state.user_id = 1 @state.user_id # = 1 As long as it's not a method on Hash, you can use it as a getter/setter. You can save anything that can be serialized using Marshal: Marshal.dump(123) # = \x04\bi\x01{ Marshal.load(\x04\bi\x01{) # = 123 -- // Magnus Holm ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list
Re: first app - some questions
On Saturday, May 3, 2014, Sebastjan Hribar sebastjan.hri...@gmail.com wrote: Hi guys, just a little feedback from a beginner. I've finished my first camping app. Thank you for your help and guidance. The finished app is about quality management and enables users to fill out forms. Based on those forms different quality parameters are recorded per user and per project. These can be called up in summary reports for desired time frame and user. Along with all the forms for a period the average quality evaluation is also displayed. Reports can only be generated by quality managers. I've managed to reuse only one html form for creating, viewing and editing. In addition, I've setup the form in such a way, that submitting is possible only when the form was called via »new« or »edit« route and the user has appropriate authorizations. I have one question regarding the session. I don't actually know how to leverage or use it. According to the reference the session adds states to the app. Can someone explain a bit more? Sorry for the late reply. Here's an example of state: module App::Controllers class Index def get if @state.user_id Welcome user number #{@state.user_id} else render :login end end class Login def post # Just log everyone in. No passwords here! @state.user_id = @input.user_id redirect Index end end end module App::Views def login form action: R(Login), method: :post do label do p User ID: input name: :user_id end button Log in end end end Here's an example of the flow when the user visits the site: 1. User visits GET /. Index#get is called. 2. The state is empty, so the #login view is rendered 3. The user enters his user ID (let's say 5). 4. When the user presses Log in, the browser will do a POST /login with user_id=5 as parameters 5. Login#post is invoked. This sets the state variable and then redirects to /. 6. The user's browser then shows GET /. Now he gets a welcome message, not the login form. Notice how the same request is done in both step 1 and 6 (GET /), but the second time the user gets a completely different page. That's because the state is different. You might know about cookies: sessions are like cookies that only the server know how to set. It's impossible for the client to set its own session; every session has to be set through the @state-variable in an action. I hope this clarifies state/sessions a bit. Don't hesitate to ask more if you're confused. // Magnus -- // Magnus Holm ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list
Re: first app - some questions
Hi guys, just a little feedback from a beginner. I've finished my first camping app. Thank you for your help and guidance. The finished app is about quality management and enables users to fill out forms. Based on those forms different quality parameters are recorded per user and per project. These can be called up in summary reports for desired time frame and user. Along with all the forms for a period the average quality evaluation is also displayed. Reports can only be generated by quality managers. I've managed to reuse only one html form for creating, viewing and editing. In addition, I've setup the form in such a way, that submitting is possible only when the form was called via »new« or »edit« route and the user has appropriate authorizations. I have one question regarding the session. I don't actually know how to leverage or use it. According to the reference the session adds states to the app. Can someone explain a bit more? regards, seba On 22. 04. 2014 21:27, Magnus Holm wrote: On Mon, Apr 21, 2014 at 7:32 PM, Sebastjan Hribar sebastjan.hri...@gmail.com wrote: Hi, I've updated the gist with my next version: https://gist.github.com/sebastjan-hribar/11081389 It works as I wanted but for now I left out the styling. My question is, is this the correct way of setting up routes? That looks fine. Although you can even simplify the Index-route like this: class Index # look, no R '/' end Can someone have a look and point out mistakes I've made. I mean the app works, but I'd like to know if (of better yet, how) the routing or anything else can be optimized. It looks fine to me :-) Secondly, when I'm done I'll need this to be running in a closed network. I'd need some advice on deployment. I can run it on a spare machine or a server even, but how what kind of configuration is required (ports, databases)...? If you want to run it on your own machine, I recommend using Phusion Passenger. Passenger (and other Ruby web server) requires a config.ru like this: $LOAD_PATH File.expand_path('..') require 'review' Review.create # remember to call your #create method here! run Review You can test this config.ru locally using the `rackup` tool: $ rackup -p 3301 If this works correctly you should also be able to point Passenger to the same file and everything should work properly! You can also deploy using Heroku when you have a working config.ru. As for configuration, the simplest thing is just checking for an environment variable in Review.create: def Review.create case ENV['RACK_ENV'] when 'production' # setup for production else # setup for development end end regards, seba ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list
Re: first app - some questions
Thank you very much for this valuable information. And I've stumble upon another problem: migrations. I want to add additional columns and my table in the database which is already set up (this is still development). I did as it's described in the camping book and put another migration with increased version number (I've deleted most columns here for brevity): class BasicFields V 1.0 def self.up create_table Reviewform.table_name do |t| t.string:title t.string:translator t.timestamps end end def self.down drop_table Reviewform.table_name end end class AdditionalBasicFields V 1.1 def self.change add_column Reviewform.table_name, :wrong_translation, :string Reviewform.reset_column_information end def self.down drop_table Reviewform.table_name end end --- And at the end of the file I have as usual: --- def Review.create Review::Models.create_schema end -- This throws this error at me when creating a new form: ActiveRecord::UnknownAttributeError at /form/new unknown attribute: wrong_translation Ruby /home/sebastjan/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/attribute_assignment.rb: in rescue in _assign_attribute, line 47 WebPOST localhost/form/new I also tried doing this as described in ActiveRecord API: class AdditionalBasicFields V 1.1 def self.change change_table :reviewforms do |t| t.integer :wrong_translation end end def self.down drop_table Reviewform.table_name end end -- And I get the same error. The table is not changed. I can't find the mistake I've made. Need help:( regards, seba ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list
Re: first app - some questions
On Mon, Apr 21, 2014 at 7:32 PM, Sebastjan Hribar sebastjan.hri...@gmail.com wrote: Hi, I've updated the gist with my next version: https://gist.github.com/sebastjan-hribar/11081389 It works as I wanted but for now I left out the styling. My question is, is this the correct way of setting up routes? That looks fine. Although you can even simplify the Index-route like this: class Index # look, no R '/' end Can someone have a look and point out mistakes I've made. I mean the app works, but I'd like to know if (of better yet, how) the routing or anything else can be optimized. It looks fine to me :-) Secondly, when I'm done I'll need this to be running in a closed network. I'd need some advice on deployment. I can run it on a spare machine or a server even, but how what kind of configuration is required (ports, databases)...? If you want to run it on your own machine, I recommend using Phusion Passenger. Passenger (and other Ruby web server) requires a config.ru like this: $LOAD_PATH File.expand_path('..') require 'review' Review.create # remember to call your #create method here! run Review You can test this config.ru locally using the `rackup` tool: $ rackup -p 3301 If this works correctly you should also be able to point Passenger to the same file and everything should work properly! You can also deploy using Heroku when you have a working config.ru. As for configuration, the simplest thing is just checking for an environment variable in Review.create: def Review.create case ENV['RACK_ENV'] when 'production' # setup for production else # setup for development end end regards, seba ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list
Re: first app - some questions
Hi, I've updated the gist with my next version: https://gist.github.com/sebastjan-hribar/11081389 It works as I wanted but for now I left out the styling. My question is, is this the correct way of setting up routes? Can someone have a look and point out mistakes I've made. I mean the app works, but I'd like to know if (of better yet, how) the routing or anything else can be optimized. Secondly, when I'm done I'll need this to be running in a closed network. I'd need some advice on deployment. I can run it on a spare machine or a server even, but how what kind of configuration is required (ports, databases)...? regards, seba On 19. 04. 2014 13:22, Sebastjan Hribar wrote: Hi, I have a couple of questions: 1. What is the difference between these two ways of setting up migrations: 1.1 example from the camping book: - |class BasicFields V 1.0| |||def self.up| |||create_table Page.table_name do |t|| |.| |.| |.| - 1.2 Example from the blog example: class BasicFields V 1.1 def self.up create_table :blog_posts, :force = true do |t| --- Why the difference between Model.table_name and :campingapp_model and which one should I use? 2. UTF-8 encoding doesn't work. Special characters like c(, s(, z( from my language are not displayed. Should I force the encoding and how? 3. Here is the link to the gist where I've posted my first camping app skeleton for the quality reporting app I need to do. I'd really appreciate it if someone can have a look and just tell me if I'm on the right track with the understanding of the camping concept. https://gist.github.com/sebastjan-hribar/11081389 ...and Happy Easter! seba ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list
Re: first app - some questions
On Sat, 19 Apr 2014 13:22:52 +0200, Sebastjan Hribar sebastjan.hri...@gmail.com wrote: 2. UTF-8 encoding doesn't work. Special characters like č, š, ž from my language are not displayed. Should I force the encoding and how? You need to do two things: a) Ensure that the source text is, in fact, encoded in UTF-8. Your editor should provide a way to change the encoding used, most sane ones use UTF-8 by default nowadays, though. b) Ensure that a 'meta charset=UTF-8 /' tag (or equivalent, there are many variants, all of which work) is present on the HTML output of your application. Otherwise browsers try to guess the encoding used and usually get it wrong. Looking at the source code you linked, you have the following in the `layout` method: html do title 'Aplikacija za dvojezične obrazce' body do self yield end end This should probably be: html do head do title 'Aplikacija za dvojezične obrazce' end body do self yield end end I *think* the latest mab adds the meta tag I mentioned automatically if you call `head`, but I'm not entirely sure – just check :), and if it doesn't, add `meta charset: 'UTF-8'` just after `head do` yourself. -- Matma Rex ___ Camping-list mailing list Camping-list@rubyforge.org http://rubyforge.org/mailman/listinfo/camping-list