Search Results

Search found 9437 results on 378 pages for 'rails newbie'.

Page 117/378 | < Previous Page | 113 114 115 116 117 118 119 120 121 122 123 124  | Next Page >

  • Rails associations of user/post/comment

    - by garthcn
    Hi, I'm trying to create an app like a blog, with 3 models: user, post and comment. As expected, a comment belongs to both a user and a post. I used the following associations: User.rb has_many :comments has_many :posts Post.rb has_many :comments belongs_to :user Comment.rb belongs_to :user belongs_to :post And I tried to create comments using: @user.comments.create However, this will relate the comment with user, but not with post. I want the comment to be associated wit BOTH user and post. Is there a way to do so? Or did I use the wrong associations? I think it might be a bad practice to set the user_id or post_id by hand, so both ids are not in attr_accessible. I'm not sure if it is correct. Thank you!

    Read the article

  • Rails Routes :requirements

    - by Chris Kilmer
    I want to set a route :requirements on an array that verifies a particular parameter is included in an array: atypes = [:culture, :personality, :communication] map.with_options(:path_prefix = ':atype', :requirements = {:atype = atypes.include?(:atype)}) do |assessment| ... end I haven't been able to find any documentation on how to accomplish this. Any help would be appreciated.

    Read the article

  • Deploying Rails app over VPN

    - by DavidGouge
    You'll have to bear with me as I'm not a Ruby dev, but have inherited a Ruby system. I need to deploy some changes to the app from my repository to the server. I've been instructed to run cap deploy and told that that script will get the latest code from my repository and deploy it to the server. My problem is that I have to VPN to get to the production server and the VPN client then blocks access to my local network, cutting off the repository. So my question is, how can I change my deploy.rb so that I can deploy from my local machine instead? Or is there a better way. If you need to see the deploy.rb, please let me know. Thanks Dave

    Read the article

  • [Rails] Accessing error_messages on form_tag

    - by aaronrussell
    I have built a custom form for creating a joining model on a has_many :through relationship. The models look roughly like this: class Team has_many :team_members has_many :members, :through => :team_members end class Member has_many :team_members has_many :teams, :through => :team_members end class TeamMember belongs_to :team belongs_to :member # and this model has some validations too end The form I have built is for selecting which members should be in a team. I won't paste the form, but it uses the form_tag method and basically sends an array of hashes which contain a member_id and a squad_number. I then update the database with an action that looks roughly like this (simplified a bit, but you get the jist): @team.transaction do @team.team_members = params[:team_members].collect{|tm| @team.team_members.new(tm)} if @team.save redirect_to ... else render :action => :members end end Everything works great but I am validating the squad_number for uniqueness and numerically. So, when any of those validations fail, how do I get access to them in my view, and how do I ascertain which of the many members it has failed on?

    Read the article

  • Rails - using :include to find objects based on their child's attributes

    - by adam
    I have a sentence and correction model class Sentence < ActiveRecord::Base has_one :correction class Correction < ActiveRecord::Base belongs_to :sentence and I'm trying find all sentences which don't have a correction. To do this I'm simply looking for corrections which don't exist i.e. whose id = nil. But it is failing and i can't figure out why Sentence.find :all, :include => :correction, :conditions => {:correction => {:id => nil}} Perhaps its the syntax or maybe just the overall approach. Can anyone help?

    Read the article

  • Speed of running a test suite in Rails

    - by Milan Novota
    I have 357 tests (534 assertions) for my app (using Shoulda). The whole test suite runs in around 80 seconds. Is this time OK? I'm just curious, since this is one of my first apps where I write tests extensively. No fancy stuff in my app. Btw.: I tried to use in memory sqlite3 database, but the results were surprisingly worse (around 83 seconds). Any clues here? I'm using Macbook with 2GB of RAM and 2GHz Intel Core Duo processor as my development machine.

    Read the article

  • Building a calendar navigation in Rails (controller and view links)

    - by user532339
    Trying to get the next month when clicking the link_to. I've done the following in the view. <%= form_tag rota_days_path, :method => 'get' do %> <p> <%= hidden_field_tag(:next_month, @t1) %> <%= link_to 'Next Month', rota_days_path(:next_month => @next_month)%> </p> <% end %> class RotaDaysController < ApplicationController # GET /rota_days # GET /rota_days.json # load_and_authorize_resource respond_to :json, :html def index @rota_days = RotaDay.all @hospitals = Hospital.all @t1 = Date.today.at_beginning_of_month @t2 = Date.today.end_of_month @dates = (@t1..@t2) #Concat variable t1 + t2 together # @next_month = Date.today + 1.month(params[: ??? ] #Old if params[:next_month] # @next_month = Date.today >> 1 @next_month = params[:next_month] + 1.month @t1 = @next_month.at_beginning_of_month @t2 = @next_month.end_of_month @dates = (@t1..@t2) end @title = "Rota" respond_to do |format| format.html # index.html.erb format.json { render json: @rota_days } end end I have identified that the reason why this may not be working is in because of the following in my controller @next_month = params[:next_month] + 1.month the last two called methods is defined only on time/date objects. but not on fixnum/string objects. I understand I am missing something from this Update I have found that the actual issue is that the `params[:next_month] is a string and I am trying to add a date to to it. Which means I need to convert the string to a date/time object. Console output: Started GET "/rota_days" for 127.0.0.1 at 2012-12-14 22:14:36 +0000 Processing by RotaDaysController#index as HTML User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 RotaDay Load (0.0ms) SELECT `rota_days`.* FROM `rota_days` Hospital Load (1.0ms) SELECT `hospitals`.* FROM `hospitals` Rendered rota_days/index.html.erb within layouts/application (23.0ms) Role Load (0.0ms) SELECT `roles`.* FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 1 AND `roles`.`name` = 'Administrator' LIMIT 1 Completed 200 OK in 42ms (Views: 39.0ms | ActiveRecord: 1.0ms)

    Read the article

  • Rails: How to have dynamic association

    - by Aaron Dufall
    I'll use an example to explain what behaviour I would like to achieve. If you had a project management app and you added a task, but not all the contributors are users of the app. So when you adding contributors to the task you can enter a user name or email address. Here is the part that I'm finding a little tricky. The task model has many contributors which are linked through the user model, but from this point on I want to achieve 2 things. Store the non members email(this would obviously be quite simple) If that email address was to create an account it would then link that user to the task and remove the temporally saved email. This way, when that user creates an account all the related tasks will already be associated with their email. Is this something that i could achieve with a polymorphic association? or is there something else I should be looking at?

    Read the article

  • Rails: find_by, conserving leading whitespaces

    - by peppermonkey
    Hi, when I do the following def somefunction @texts = A.find_all_by_someid(someid) respond_to do |format| format.xml { render :xml => @texts } end end it gets the string from the db correctly, except if the string has leading whitespaces, it seems they are trimmed. Note: the whitespaces are there in the db correctly What can I do to conserve those whitespaces? Thanks

    Read the article

  • rails route question

    - by badnaam
    I am trying to build a search functionality which at a high level works like this. 1 - I have a Search model, controller with a search_set action and search views/partial to render the search. 2 - At the home page a serach form is loaded with an empty search object or a search object initialized with session[:search] (which contains user search preferences, zip code, proximity, sort order, per page etc). This form has a post(:put) action to search_set. 3 - When a registered user performs a set the params of the search form are collected and a search record is saved against that user. If a unregistered user performs a search then the search set action simply stores the params in the session[:search]. In either case, the search is executed with the given params and the results are displayed. At this point the url of in the location bar is something like.. http://localhost:3000/searches/search_set?stype=1 At this point if the user simply hits enter on the location bar, I get an error that says "No action responded to show" I am guessing because the URL contains search_set which uses a put method and even though I have a search_show (:get) action (which simply reruns the search in the session or saved in the database) does not get called. How can I handle this situation where I can route a user hitting enter into the location bar to a get method? If this does not explain the problem , please let me know I can share more details/code etc. Thanks!

    Read the article

  • Ruby on Rails: create records for multiple models with one form and one submit

    - by notblakeshelton
    I have a 3 models: quote, customer, and item. Each quote has one customer and one item. I would like to create a new quote, a new customer, and a new item in their respective tables when I press the submit button. I have looked at other questions and railscasts and either they don't work for my situation or I don't know how to implement them. I also want my index page to be the page where I can create everything. quote.rb class Quote < ActiveRecord::Base attr_accessible :quote_number has_one :customer has_one :item end customer.rb class Customer < ActiveRecord::Base #unsure of what to put here #a customer can have multiple quotes, so would i use: has_many :quotes #<----? end item.rb class Item < ActiveRecord::Base #also unsure about this #each item can also be in multiple quotes quotes_controller.rb class QuotesController < ApplicationController def index @quote = Quote.new @customer = Customer.new @item = item.new end def create @quote = Quote.new(params[:quote]) @quote.save @customer = Customer.new(params[:customer]) @customer.save @item = Item.new(params[:item]) @item.save end end items_controller.rb class ItemsController < ApplicationController def index end def new @item = Item.new end def create @item = Item.new(params[:item]) @item.save end end customers_controller.rb class CustomersController < ApplicationController def index end def new @customer = Customer.new end def create @customer = Customer.new(params[:customer]) @customer.save end end quotes/index.html.erb <%= form_for @quote do |f| %> <%= f.fields_for @customer do |builder| %> <%= label_tag :firstname %> <%= builder.text_field :firstname %> <%= label_tag :lastname %> <%= builder.text_field :lastname %> <% end %> <%= f.fields_for @item do |builder| %> <%= label_tag :name %> <%= builder.text_field :name %> <%= label_tag :description %> <%= builder.text_field :description %> <% end %> <%= label_tag :quote_number %> <%= f.text_field :quote_number %> <%= f.submit %> <% end %> When I try submitting that I get an error: Can't mass-assign protected attributes: item, customer So to try and fix it I updated the attr_accessible in quote.rb to include :item, :customer but then I get this error: Item(#) expected, got ActiveSupport::HashWithIndifferentAccess(#) Any help would be greatly appreciated.

    Read the article

  • Ruby on rails 2 level model

    - by jony17
    I need some help creating a very simple forum in a existing model. What I want in a Game page, have a mini forum, where is possible create some topics and some comments to this topics. In the beginning I'm only implement topics. This is the error I have: Mysql2::Error: Column 'user_id' cannot be null: INSERT INTO `topics` (`game_id`, `question`, `user_id`) VALUES (1, 'asd', NULL) This is my main model: game.rb class Game < ActiveRecord::Base attr_accessible :name validates :user_id, presence: true validates :name, presence: true, length: { maximum: 50 } belongs_to :user has_many :topics, dependent: :destroy end topic.rb class Topic < ActiveRecord::Base validates_presence_of :question validates_presence_of :game_id attr_accessible :question, :user_id validates :question, length: {maximum: 50}, allow_blank: false belongs_to :game belongs_to :user end topic_controller.rb def create @game = Game.find(params[:game_id]) @topic = @game.topics.create(params[:topic]) @topic.user_id = current_user.id respond_to do |format| if @topic.save format.html { redirect_to @game, notice: 'Topic was successfully created.' } else format.html { render action: "new" } end end end game/show.html.erb <h2>Topics</h2> <% @game.topics.each do |topic| %> <p> <b>Question:</b> <%= topic.question %> </p> <% end %> <h2>Add a topic:</h2> <%= form_for([@game, @game.topics.build]) do |f| %> <div class="field"> <%= f.label :question %><br /> <%= f.text_field :question %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> Thanks ;)

    Read the article

  • Rails creating and updating 2 model records simultaneously

    - by LearnRails
    I have 2 tables product and history product table id name type price location 1 abc electronics $200 aisle1 history table id product_id status 1 1 price changed from $200 to $180 Whenever the product price or location is updated by a user by hitting the update button, 1) the changes should be automatically be reflected in the history status column without the user having to enter that manually. if the price is updated from 200 to 180 then a new history row will be created with new id and the status column will say ' price changed from $200 to $180' if the location is updated from aisle1 to aisle 2 then status displays ' loc changed from ailse1 to aisle 2' I tried to @product = Product.new(params[:product]) @history= History.new(params[:history]) if @product.save @history.new(attributes) == I am not sure of whether this approach is correct I would really appreciate if someone could tell me how the history can be automatically updated in this case.

    Read the article

  • Ruby on Rails: Best way to save search queries in a database

    - by Adam Templeton
    For a RoR app I'm helping develop, I need to save all search queries in a database so I can analyze them later. My plan right now is to create a Result model and table, and just save each search query's text in that table, along with a user's ID, the time, etc. However, the app has about 15,000 users, so I'm afraid the single table approach won't be super efficient when it comes time to parse that data. (The database is setup via MySQL, if that factors in at all.) Am I just being paranoid? Is there a Ruby gem that handles this sort of thing, or a better approach I could take? Any input would be appreciated.

    Read the article

  • Rails routing aliasing and namespaces

    - by kain
    Given a simple namespaced route map.namespace :api do |api| api.resources :genres end how can I reuse this block but with another namespace? Currently I'm achieving that by writing another routes hacked on the fly map.with_options :name_prefix => 'mobile_', :path_prefix => 'mobile' do |mobile| mobile.resources :genres, :controller => 'api/genres' end But it seems less than ideal.

    Read the article

  • Rails how to return a list of answers with a specific question_id

    - by mytwocentsisworthtwocents
    Let's say I have two Models, Answers and Questions (there are others, but irrelevant to the question). The models are as follows: Answer.rb class Answer < ActiveRecord::Base attr_accessible :description, :question_id has_one :question, :through => :user, :dependent => :destroy validates :description, :presence => true end Question.rb class Question < ActiveRecord::Base attr_accessible :budget, :description, :headline, :user_id, :updated_at, :created_at belongs_to :user has_many :answers validates :headline, :description, :user_id, :presence => true end I'd like to display on a page a list of all answers associated with a question, and only those questions. I got this far. I believe this variable finds all the questions in the database by the question_id (foreign key): @findanswers = Answer.all(params[:question_id]) And this one grabs the the id of the current question (this code will reside as an ERB on the page where the current question is located): @questionshow = Question.find(params[:id]) And now I'm stuck. How do I put the two together so that I list all the answers with the current question id?

    Read the article

  • rails eval code

    - by xpepermint
    Hey. I have to do a coll inside my model like this: import = Import.find(id) status = User.find(import.user_id).{XXX}.import(import.file.path) Notice a {XXX} which should be replaced by a dinamic variable of a submodel. Model User has_many groups, clients and products. In translation this would be status = User.find(import.user_id).groups.import(import.file.path) status = User.find(import.user_id).clients.import(import.file.path) status = User.find(import.user_id).products.import(import.file.path) I was thinking of import = Import.find(id) status = eval("User.find(#{import.user_id}).#{import.model}").import(import.file.path) but this gives me an error 'TypeError: can't convert nil into String'. Please tell me how would you fix that. Thx!

    Read the article

  • Static selection and Ruby on Rails objects

    - by Dave
    Hi all- I have a simple problem, but am having trouble wrapping my head around it. I have an video object that should have one or more "genres". This list of genres should be prepopulated and then the user should just select one or more using autocomplete or some such. Here is the question: Is it worth creating a table with genres for the static selection? Or should it just be included in the presentation layer? If there is a static table, how do we name it correctly. I envision something like this class Video < ActiveRecord::Base has_many :genres ... end class Genre < ... belongs_to :video ... end But then we get a table called genre, that basically maps all the selected genres to their parent videos. There would need to be some static table to reference the static genres. Is this the best way to do it? Sorry if this was rambl-y a little stream of conciousness. Thanks!

    Read the article

  • Providing updates during a long Rails controller action

    - by highBandWidth
    I have an action that takes a long time. I want to be able to provide updates during the process so the user is not confused as to whether he lost the connection or something. Can I do something like this: class HeavyLiftingController < ApplicationController def data_mine render_update :js=>"alert('Just starting!')" # do some complicated find etc. render_update :js=>"alert('Found the records!')" # do some processing ... render_update :js=>"alert('Done processig')" # send @results to view end end

    Read the article

  • Rails query to find other likes based on category

    - by Mik
    I need to do something like the following: Find other categories people like based on the categories you like. I have a likes table, which is joined to users and categories. I need to do an efficient query to find out what other categories people who liked a given category also like. Any thoughts on this would be appreciated. Thanks in advance.

    Read the article

  • JQuery + Rails + Select Menu

    - by blackpond
    I want to have a select menu to change a field on a Customer dynamically, I've never used Jquery with a select menu, and I'm having problems. The code: <% form_for @customer , :url = { :action = "update" }, :html ={:class = "ajax_form"} do |f| % Pricing: <%= select :customer, :pricing, Customer::PRICING, {}, :onchange = "$('this').closest('form').submit();" % Application.js: $(document).ready(function(){ $(".ajax_link").live("click",function(event){ //take div class = ajax_link and call this funciton when clicked. event.preventDefault(); // cancels http request $.post($(this).attr("href"), null, null, "script"); return false; }); ajaxFormSubmitHooks(); }); function ajaxFormSubmitHooks(){ $(".ajax_form").submit(function(event){ event.preventDefault(); // cancels http request $.post($(this).attr("action"), $(this).serializeArray(), null, "script"); return false; }); }

    Read the article

< Previous Page | 113 114 115 116 117 118 119 120 121 122 123 124  | Next Page >